Skip to content

Handle contentUri in copy function #165

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
39 changes: 32 additions & 7 deletions android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.RNFetchBlob;

import android.content.ContentResolver;
import android.content.res.AssetFileDescriptor;
import android.media.MediaScannerConnection;
import android.net.Uri;
Expand Down Expand Up @@ -663,19 +664,30 @@ 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);
}
}

/**
* 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 {
Expand Down Expand Up @@ -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));
}
Expand All @@ -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();
}

Expand Down
12 changes: 10 additions & 2 deletions android/src/main/java/com/RNFetchBlob/Utils/PathResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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)) {
Expand All @@ -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=?";
Expand Down Expand Up @@ -212,4 +220,4 @@ public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}

}
}