Skip to content

Commit 883005a

Browse files
Recommitting fixes done on original fork
1 parent 9110ef2 commit 883005a

File tree

1 file changed

+74
-35
lines changed

1 file changed

+74
-35
lines changed

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

Lines changed: 74 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
import android.content.ContentUris;
1111
import android.os.Environment;
1212
import android.content.ContentResolver;
13+
14+
import androidx.annotation.Nullable;
15+
1316
import com.RNFetchBlob.RNFetchBlobUtils;
1417
import java.io.File;
18+
import java.io.IOException;
1519
import java.io.InputStream;
1620
import java.io.FileOutputStream;
1721

@@ -41,15 +45,15 @@ else if (isDownloadsDocument(uri)) {
4145
final String id = DocumentsContract.getDocumentId(uri);
4246
//Starting with Android O, this "id" is not necessarily a long (row number),
4347
//but might also be a "raw:/some/file/path" URL
44-
if (id != null && id.startsWith("raw:/")) {
45-
Uri rawuri = Uri.parse(id);
46-
String path = rawuri.getPath();
47-
return path;
48+
if (id != null) {
49+
if (id.startsWith("raw:/")) {
50+
Uri rawUri = Uri.parse(id);
51+
return rawUri.getPath();
52+
}
53+
final Uri contentUri = ContentUris.withAppendedId(
54+
Uri.parse("content://downloads/public_downloads"), Long.parseLong(id));
55+
return getDataColumn(context, contentUri, null, null);
4856
}
49-
final Uri contentUri = ContentUris.withAppendedId(
50-
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
51-
52-
return getDataColumn(context, contentUri, null, null);
5357
}
5458
catch (Exception ex) {
5559
//something went wrong, but android should still be able to handle the original uri by returning null here (see readFile(...))
@@ -85,30 +89,16 @@ else if ("content".equalsIgnoreCase(uri.getScheme())) {
8589
if (isGooglePhotosUri(uri))
8690
return uri.getLastPathSegment();
8791

88-
return getDataColumn(context, uri, null, null);
92+
String result = getDataColumn(context, uri, null, null);
93+
if (result != null) {
94+
return result;
95+
} else {
96+
return getPathByDownloadingFromUri(context, uri);
97+
}
8998
}
9099
// Other Providers
91100
else{
92-
try {
93-
InputStream attachment = context.getContentResolver().openInputStream(uri);
94-
if (attachment != null) {
95-
String filename = getContentName(context.getContentResolver(), uri);
96-
if (filename != null) {
97-
File file = new File(context.getCacheDir(), filename);
98-
FileOutputStream tmp = new FileOutputStream(file);
99-
byte[] buffer = new byte[1024];
100-
while (attachment.read(buffer) > 0) {
101-
tmp.write(buffer);
102-
}
103-
tmp.close();
104-
attachment.close();
105-
return file.getAbsolutePath();
106-
}
107-
}
108-
} catch (Exception e) {
109-
RNFetchBlobUtils.emitWarningEvent(e.toString());
110-
return null;
111-
}
101+
return getPathByDownloadingFromUri(context, uri);
112102
}
113103
}
114104
// MediaStore (and general)
@@ -128,14 +118,17 @@ else if ("file".equalsIgnoreCase(uri.getScheme())) {
128118
return null;
129119
}
130120

121+
@Nullable
131122
private static String getContentName(ContentResolver resolver, Uri uri) {
132123
Cursor cursor = resolver.query(uri, null, null, null, null);
133-
cursor.moveToFirst();
134-
int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
135-
if (nameIndex >= 0) {
136-
String name = cursor.getString(nameIndex);
137-
cursor.close();
138-
return name;
124+
if (cursor != null) {
125+
cursor.moveToFirst();
126+
int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
127+
if (nameIndex >= 0) {
128+
String name = cursor.getString(nameIndex);
129+
cursor.close();
130+
return name;
131+
}
139132
}
140133
return null;
141134
}
@@ -179,6 +172,52 @@ public static String getDataColumn(Context context, Uri uri, String selection,
179172
return result;
180173
}
181174

175+
@Nullable
176+
private static String getPathByDownloadingFromUri(Context context, Uri uri) {
177+
InputStream attachment = null;
178+
try {
179+
attachment = context.getContentResolver().openInputStream(uri);
180+
if (attachment != null) {
181+
String fileName = getContentName(context.getContentResolver(), uri);
182+
if (fileName != null) {
183+
File file = new File(context.getCacheDir(), fileName);
184+
FileOutputStream tmp = new FileOutputStream(file);
185+
try {
186+
byte[] buffer =new byte[1024];
187+
while (attachment.read(buffer) > 0) {
188+
tmp.write(buffer);
189+
}
190+
} catch (Exception e) {
191+
e.printStackTrace();
192+
RNFetchBlobUtils.emitWarningEvent(e.toString());
193+
} finally {
194+
try {
195+
tmp.close();
196+
} catch (IOException e) {
197+
e.printStackTrace();
198+
RNFetchBlobUtils.emitWarningEvent(e.toString());
199+
}
200+
}
201+
attachment.close();
202+
return file.getAbsolutePath();
203+
}
204+
}
205+
} catch (Exception e) {
206+
e.printStackTrace();
207+
RNFetchBlobUtils.emitWarningEvent(e.toString());
208+
return null;
209+
} finally {
210+
if (attachment != null) {
211+
try {
212+
attachment.close();
213+
} catch (IOException e) {
214+
e.printStackTrace();
215+
RNFetchBlobUtils.emitWarningEvent(e.toString());
216+
}
217+
}
218+
}
219+
return null;
220+
}
182221

183222
/**
184223
* @param uri The Uri to check.

0 commit comments

Comments
 (0)