Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): file path correction if Uri authority is FileProvider #585

Merged
merged 2 commits into from
Aug 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions src/android/FileHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.Context;
import android.content.CursorLoader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
Expand All @@ -29,8 +28,8 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.webkit.MimeTypeMap;

import org.apache.cordova.CordovaInterface;
import org.apache.cordova.LOG;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -44,7 +43,7 @@ public class FileHelper {
* Returns the real path of the given URI string.
* If the given URI string represents a content:// URI, the real path is retrieved from the media store.
*
* @param uriString the URI string of the audio/image/video
* @param uri the URI of the audio/image/video
* @param cordova the current application context
* @return the full path to the file
*/
Expand All @@ -66,7 +65,7 @@ public static String getRealPath(Uri uri, CordovaInterface cordova) {
* Returns the real path of the given URI.
* If the given URI is a content:// URI, the real path is retrieved from the media store.
*
* @param uri the URI of the audio/image/video
* @param uriString the URI string from which to obtain the input stream
* @param cordova the current application context
* @return the full path to the file
*/
Expand Down Expand Up @@ -143,6 +142,9 @@ else if ("content".equalsIgnoreCase(uri.getScheme())) {
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();

if (isFileProviderUri(context, uri))
return getFileProviderPath(context, uri);

return getDataColumn(context, uri, null, null);
}
// File
Expand Down Expand Up @@ -188,6 +190,7 @@ public static InputStream getInputStreamFromUriString(String uriString, CordovaI
if (question > -1) {
uriString = uriString.substring(0, question);
}

if (uriString.startsWith("file:///android_asset/")) {
Uri uri = Uri.parse(uriString);
String relativePath = uri.getPath().substring(15);
Expand Down Expand Up @@ -217,6 +220,7 @@ public static InputStream getInputStreamFromUriString(String uriString, CordovaI
* @return a path without the "file://" prefix
*/
public static String stripFileProtocol(String uriString) {

if (uriString.startsWith("file://")) {
uriString = uriString.substring(7);
}
Expand Down Expand Up @@ -327,4 +331,28 @@ public static boolean isMediaDocument(Uri uri) {
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}

/**
* @param context The Application context
* @param uri The Uri is checked by functions
* @return Whether the Uri authority is FileProvider
*/
public static boolean isFileProviderUri(final Context context, final Uri uri) {
final String packageName = context.getPackageName();
final String authority = new StringBuilder(packageName).append(".provider").toString();
return authority.equals(uri.getAuthority());
}

/**
* @param context The Application context
* @param uri The Uri is checked by functions
* @return File path or null if file is missing
*/
public static String getFileProviderPath(final Context context, final Uri uri)
{
final File appDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
final File file = new File(appDir, uri.getLastPathSegment());
return file.exists() ? file.toString(): null;
}

}