Skip to content

Commit

Permalink
fix(android): file path correction if Uri authority is FileProvider (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidWiesner committed Dec 7, 2021
1 parent 660d722 commit 26753f3
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/android/FileHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,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 @@ -345,5 +348,30 @@ public static File getExternalFilesDir(Context context) {
return filesDirs[0];
}
return null;

}

/**
* @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;
}

}

0 comments on commit 26753f3

Please sign in to comment.