Skip to content

Commit 4be9547

Browse files
committed
Handle copy from content URI using android content resolver
1 parent 0dacc63 commit 4be9547

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -519,15 +519,15 @@ static void mkdir(String path, Promise promise) {
519519
* @param callback JS context callback
520520
*/
521521
static void cp(String path, String dest, Callback callback) {
522-
path = ReactNativeBlobUtilUtils.normalizePath(path);
523522
dest = ReactNativeBlobUtilUtils.normalizePath(dest);
524523
InputStream in = null;
525524
OutputStream out = null;
526525
String message = "";
527526

528527
try {
529-
if (!isPathExists(path)) {
530-
callback.invoke("Source file at path`" + path + "` does not exist");
528+
in = inputStreamFromPath(path);
529+
if (in == null) {
530+
callback.invoke("Source file at path`" + path + "` does not exist or can not be opened");
531531
return;
532532
}
533533
if (!new File(dest).exists()) {
@@ -538,7 +538,6 @@ static void cp(String path, String dest, Callback callback) {
538538
}
539539
}
540540

541-
in = inputStreamFromPath(path);
542541
out = new FileOutputStream(dest);
543542

544543
byte[] buf = new byte[10240];
@@ -1018,8 +1017,10 @@ protected Integer doInBackground(ReadableArray... paths) {
10181017
}
10191018

10201019
/**
1021-
* Get input stream of the given path, when the path is a string starts with bundle-assets://
1022-
* the stream is created by Assets Manager, otherwise use FileInputStream.
1020+
* Get input stream of the given path.
1021+
* When the path starts with bundle-assets:// the stream is created by Assets Manager
1022+
* When the path starts with content:// the stream is created by ContentResolver
1023+
* otherwise use FileInputStream.
10231024
*
10241025
* @param path The file to open stream
10251026
* @return InputStream instance
@@ -1029,7 +1030,10 @@ private static InputStream inputStreamFromPath(String path) throws IOException {
10291030
if (path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET)) {
10301031
return ReactNativeBlobUtilImpl.RCTContext.getAssets().open(path.replace(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET, ""));
10311032
}
1032-
return new FileInputStream(new File(path));
1033+
if (path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_CONTENT)) {
1034+
return ReactNativeBlobUtilImpl.RCTContext.getContentResolver().openInputStream(Uri.parse(path));
1035+
}
1036+
return new FileInputStream(new File(ReactNativeBlobUtilUtils.normalizePath(path)));
10331037
}
10341038

10351039
/**

0 commit comments

Comments
 (0)