diff --git a/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java b/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java index aa8241dff..bfbc40ef9 100644 --- a/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java +++ b/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java @@ -1,5 +1,6 @@ package com.RNFetchBlob; +import android.content.ContentResolver; import android.content.res.AssetFileDescriptor; import android.media.MediaScannerConnection; import android.net.Uri; @@ -663,11 +664,22 @@ static void exists(String path, Callback callback) { } catch (IOException e) { callback.invoke(false, false); } - } - else { + } else if (path.startsWith(RNFetchBlobConst.FILE_PREFIX_CONTENT)) { + try { + InputStream in = RNFetchBlob.RCTContext.getContentResolver().openInputStream(Uri.parse(path)); + if (in != null) { + in.close(); + callback.invoke(true, false); + } else { + callback.invoke(false, false); + } + } catch (Exception e) { + callback.invoke(false, false); + } + } else { path = normalizePath(path); - boolean exist = new File(path).exists(); - boolean isDir = new File(path).isDirectory(); + boolean exist = path != null ? new File(path).exists() : false; + boolean isDir = path != null ? new File(path).isDirectory() : false; callback.invoke(exist, isDir); } } @@ -675,7 +687,7 @@ static void exists(String path, Callback callback) { /** * List content of folder * @param path Target folder - * @param callback JS context callback + * @param promise JS context promise */ static void ls(String path, Promise promise) { try { @@ -1093,6 +1105,8 @@ private void emitStreamEvent(String streamName, String event, String code, Strin private static InputStream inputStreamFromPath(String path) throws IOException { if (path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) { return RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "")); + } else if (path.startsWith(RNFetchBlobConst.FILE_PREFIX_CONTENT)) { + return RNFetchBlob.RCTContext.getContentResolver().openInputStream(Uri.parse(path)); } return new FileInputStream(new File(path)); } @@ -1110,8 +1124,19 @@ private static boolean isPathExists(String path) { return false; } return true; - } - else { + } else if (path.startsWith(RNFetchBlobConst.FILE_PREFIX_CONTENT)) { + try { + InputStream in = RNFetchBlob.RCTContext.getContentResolver().openInputStream(Uri.parse(path)); + if (in != null) { + in.close(); + return true; + } else { + return false; + } + } catch (Exception e) { + return false; + } + } else { return new File(path).exists(); } diff --git a/android/src/main/java/com/RNFetchBlob/Utils/PathResolver.java b/android/src/main/java/com/RNFetchBlob/Utils/PathResolver.java index c83fdbaf6..8e24513d1 100644 --- a/android/src/main/java/com/RNFetchBlob/Utils/PathResolver.java +++ b/android/src/main/java/com/RNFetchBlob/Utils/PathResolver.java @@ -10,6 +10,8 @@ import android.content.ContentUris; import android.os.Environment; import android.content.ContentResolver; +import android.text.TextUtils; + import com.RNFetchBlob.RNFetchBlobUtils; import java.io.File; import java.io.InputStream; @@ -31,6 +33,10 @@ public static String getRealPathFromURI(final Context context, final Uri uri) { if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; + } else if ("raw".equalsIgnoreCase(type)) { + return split[1]; + } else if (type != null && !TextUtils.isEmpty(type)) { + return "/storage/" + type + "/" + split[1]; } // TODO handle non-primary volumes @@ -55,7 +61,7 @@ else if (isDownloadsDocument(uri)) { //something went wrong, but android should still be able to handle the original uri by returning null here (see readFile(...)) return null; } - + } // MediaProvider else if (isMediaDocument(uri)) { @@ -70,6 +76,8 @@ else if (isMediaDocument(uri)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; + } else if ("raw".equalsIgnoreCase(type)) { + return split[1]; } final String selection = "_id=?"; @@ -212,4 +220,4 @@ public static boolean isGooglePhotosUri(Uri uri) { return "com.google.android.apps.photos.content".equals(uri.getAuthority()); } -} \ No newline at end of file +}