@@ -19,7 +19,6 @@ Licensed to the Apache Software Foundation (ASF) under one
19
19
import android .annotation .SuppressLint ;
20
20
import android .content .ContentUris ;
21
21
import android .content .Context ;
22
- import android .content .CursorLoader ;
23
22
import android .database .Cursor ;
24
23
import android .net .Uri ;
25
24
import android .os .Build ;
@@ -29,8 +28,8 @@ Licensed to the Apache Software Foundation (ASF) under one
29
28
import android .webkit .MimeTypeMap ;
30
29
31
30
import org .apache .cordova .CordovaInterface ;
32
- import org .apache .cordova .LOG ;
33
31
32
+ import java .io .File ;
34
33
import java .io .FileInputStream ;
35
34
import java .io .IOException ;
36
35
import java .io .InputStream ;
@@ -44,7 +43,7 @@ public class FileHelper {
44
43
* Returns the real path of the given URI string.
45
44
* If the given URI string represents a content:// URI, the real path is retrieved from the media store.
46
45
*
47
- * @param uriString the URI string of the audio/image/video
46
+ * @param uri the URI of the audio/image/video
48
47
* @param cordova the current application context
49
48
* @return the full path to the file
50
49
*/
@@ -57,7 +56,7 @@ public static String getRealPath(Uri uri, CordovaInterface cordova) {
57
56
* Returns the real path of the given URI.
58
57
* If the given URI is a content:// URI, the real path is retrieved from the media store.
59
58
*
60
- * @param uri the URI of the audio/image/video
59
+ * @param uriString the URI string from which to obtain the input stream
61
60
* @param cordova the current application context
62
61
* @return the full path to the file
63
62
*/
@@ -132,6 +131,9 @@ else if ("content".equalsIgnoreCase(uri.getScheme())) {
132
131
if (isGooglePhotosUri (uri ))
133
132
return uri .getLastPathSegment ();
134
133
134
+ if (isFileProviderUri (context , uri ))
135
+ return getFileProviderPath (context , uri );
136
+
135
137
return getDataColumn (context , uri , null , null );
136
138
}
137
139
// File
@@ -161,6 +163,7 @@ public static InputStream getInputStreamFromUriString(String uriString, CordovaI
161
163
if (question > -1 ) {
162
164
uriString = uriString .substring (0 , question );
163
165
}
166
+
164
167
if (uriString .startsWith ("file:///android_asset/" )) {
165
168
Uri uri = Uri .parse (uriString );
166
169
String relativePath = uri .getPath ().substring (15 );
@@ -190,6 +193,7 @@ public static InputStream getInputStreamFromUriString(String uriString, CordovaI
190
193
* @return a path without the "file://" prefix
191
194
*/
192
195
public static String stripFileProtocol (String uriString ) {
196
+
193
197
if (uriString .startsWith ("file://" )) {
194
198
uriString = uriString .substring (7 );
195
199
}
@@ -300,4 +304,28 @@ public static boolean isMediaDocument(Uri uri) {
300
304
public static boolean isGooglePhotosUri (Uri uri ) {
301
305
return "com.google.android.apps.photos.content" .equals (uri .getAuthority ());
302
306
}
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
+
303
331
}
0 commit comments