Skip to content

Commit c7971d9

Browse files
Suzzaktimbru31Francis Monierbreautek
authored
fix(android): file path correction if Uri authority is FileProvider (#585)
Co-authored-by: Tim Brust <[email protected]> Co-authored-by: Francis Monier <[email protected]> Co-authored-by: Norman Breau <[email protected]>
1 parent 3112e5f commit c7971d9

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/android/FileHelper.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ Licensed to the Apache Software Foundation (ASF) under one
1919
import android.annotation.SuppressLint;
2020
import android.content.ContentUris;
2121
import android.content.Context;
22-
import android.content.CursorLoader;
2322
import android.database.Cursor;
2423
import android.net.Uri;
2524
import android.os.Build;
@@ -29,8 +28,8 @@ Licensed to the Apache Software Foundation (ASF) under one
2928
import android.webkit.MimeTypeMap;
3029

3130
import org.apache.cordova.CordovaInterface;
32-
import org.apache.cordova.LOG;
3331

32+
import java.io.File;
3433
import java.io.FileInputStream;
3534
import java.io.IOException;
3635
import java.io.InputStream;
@@ -44,7 +43,7 @@ public class FileHelper {
4443
* Returns the real path of the given URI string.
4544
* If the given URI string represents a content:// URI, the real path is retrieved from the media store.
4645
*
47-
* @param uriString the URI string of the audio/image/video
46+
* @param uri the URI of the audio/image/video
4847
* @param cordova the current application context
4948
* @return the full path to the file
5049
*/
@@ -57,7 +56,7 @@ public static String getRealPath(Uri uri, CordovaInterface cordova) {
5756
* Returns the real path of the given URI.
5857
* If the given URI is a content:// URI, the real path is retrieved from the media store.
5958
*
60-
* @param uri the URI of the audio/image/video
59+
* @param uriString the URI string from which to obtain the input stream
6160
* @param cordova the current application context
6261
* @return the full path to the file
6362
*/
@@ -132,6 +131,9 @@ else if ("content".equalsIgnoreCase(uri.getScheme())) {
132131
if (isGooglePhotosUri(uri))
133132
return uri.getLastPathSegment();
134133

134+
if (isFileProviderUri(context, uri))
135+
return getFileProviderPath(context, uri);
136+
135137
return getDataColumn(context, uri, null, null);
136138
}
137139
// File
@@ -161,6 +163,7 @@ public static InputStream getInputStreamFromUriString(String uriString, CordovaI
161163
if (question > -1) {
162164
uriString = uriString.substring(0, question);
163165
}
166+
164167
if (uriString.startsWith("file:///android_asset/")) {
165168
Uri uri = Uri.parse(uriString);
166169
String relativePath = uri.getPath().substring(15);
@@ -190,6 +193,7 @@ public static InputStream getInputStreamFromUriString(String uriString, CordovaI
190193
* @return a path without the "file://" prefix
191194
*/
192195
public static String stripFileProtocol(String uriString) {
196+
193197
if (uriString.startsWith("file://")) {
194198
uriString = uriString.substring(7);
195199
}
@@ -300,4 +304,28 @@ public static boolean isMediaDocument(Uri uri) {
300304
public static boolean isGooglePhotosUri(Uri uri) {
301305
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
302306
}
307+
308+
/**
309+
* @param context The Application context
310+
* @param uri The Uri is checked by functions
311+
* @return Whether the Uri authority is FileProvider
312+
*/
313+
public static boolean isFileProviderUri(final Context context, final Uri uri) {
314+
final String packageName = context.getPackageName();
315+
final String authority = new StringBuilder(packageName).append(".provider").toString();
316+
return authority.equals(uri.getAuthority());
317+
}
318+
319+
/**
320+
* @param context The Application context
321+
* @param uri The Uri is checked by functions
322+
* @return File path or null if file is missing
323+
*/
324+
public static String getFileProviderPath(final Context context, final Uri uri)
325+
{
326+
final File appDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
327+
final File file = new File(appDir, uri.getLastPathSegment());
328+
return file.exists() ? file.toString(): null;
329+
}
330+
303331
}

0 commit comments

Comments
 (0)