Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit a8cfeb1

Browse files
committed
Correct app content provider URI handle #287
2 parents 40efd14 + 40fefd4 commit a8cfeb1

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

+23-6
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,28 @@ static public void writeFile(String path, ReadableArray data, final boolean appe
138138
* @param promise
139139
*/
140140
static public void readFile(String path, String encoding, final Promise promise ) {
141-
path = normalizePath(path);
141+
String resolved = normalizePath(path);
142+
if(resolved != null)
143+
path = resolved;
142144
try {
143145
byte[] bytes;
144146

145-
if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
147+
if(resolved != null && resolved.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
146148
String assetName = path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
147149
long length = RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength();
148150
bytes = new byte[(int) length];
149151
InputStream in = RNFetchBlob.RCTContext.getAssets().open(assetName);
150152
in.read(bytes, 0, (int) length);
151153
in.close();
152154
}
155+
// issue 287
156+
else if(resolved == null) {
157+
InputStream in = RNFetchBlob.RCTContext.getContentResolver().openInputStream(Uri.parse(path));
158+
int length = (int) in.available();
159+
bytes = new byte[length];
160+
in.read(bytes);
161+
in.close();
162+
}
153163
else {
154164
File f = new File(path);
155165
int length = (int) f.length();
@@ -226,17 +236,24 @@ static public String getTmpPath(ReactApplicationContext ctx, String taskId) {
226236
* @param bufferSize Buffer size of read stream, default to 4096 (4095 when encode is `base64`)
227237
*/
228238
public void readStream(String path, String encoding, int bufferSize, int tick, final String streamId) {
229-
path = normalizePath(path);
239+
String resolved = normalizePath(path);
240+
if(resolved != null)
241+
path = resolved;
230242
try {
231243

232244
int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
233245
if(bufferSize > 0)
234246
chunkSize = bufferSize;
235247

236248
InputStream fs;
237-
if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
238-
fs = RNFetchBlob.RCTContext.getAssets()
239-
.open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
249+
250+
if(resolved != null && path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
251+
fs = RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
252+
253+
}
254+
// fix issue 287
255+
else if(resolved == null) {
256+
fs = RNFetchBlob.RCTContext.getContentResolver().openInputStream(Uri.parse(path));
240257
}
241258
else {
242259
fs = new FileInputStream(new File(path));

android/src/main/java/com/RNFetchBlob/Utils/PathResolver.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,16 @@ else if (isMediaDocument(uri)) {
6464

6565
return getDataColumn(context, contentUri, selection, selectionArgs);
6666
}
67+
else if ("content".equalsIgnoreCase(uri.getScheme())) {
68+
69+
// Return the remote address
70+
if (isGooglePhotosUri(uri))
71+
return uri.getLastPathSegment();
72+
73+
return getDataColumn(context, uri, null, null);
74+
}
6775
// Other Providers
68-
else {
76+
else{
6977
try {
7078
InputStream attachment = context.getContentResolver().openInputStream(uri);
7179
if (attachment != null) {
@@ -131,6 +139,7 @@ public static String getDataColumn(Context context, Uri uri, String selection,
131139
String[] selectionArgs) {
132140

133141
Cursor cursor = null;
142+
String result = null;
134143
final String column = "_data";
135144
final String[] projection = {
136145
column
@@ -141,13 +150,18 @@ public static String getDataColumn(Context context, Uri uri, String selection,
141150
null);
142151
if (cursor != null && cursor.moveToFirst()) {
143152
final int index = cursor.getColumnIndexOrThrow(column);
144-
return cursor.getString(index);
153+
result = cursor.getString(index);
145154
}
146-
} finally {
155+
}
156+
catch (Exception ex) {
157+
ex.printStackTrace();
158+
return null;
159+
}
160+
finally {
147161
if (cursor != null)
148162
cursor.close();
149163
}
150-
return null;
164+
return result;
151165
}
152166

153167

0 commit comments

Comments
 (0)