10
10
import android .content .ContentUris ;
11
11
import android .os .Environment ;
12
12
import android .content .ContentResolver ;
13
+
14
+ import androidx .annotation .Nullable ;
15
+
13
16
import com .RNFetchBlob .RNFetchBlobUtils ;
14
17
import java .io .File ;
18
+ import java .io .IOException ;
15
19
import java .io .InputStream ;
16
20
import java .io .FileOutputStream ;
17
21
@@ -41,15 +45,15 @@ else if (isDownloadsDocument(uri)) {
41
45
final String id = DocumentsContract .getDocumentId (uri );
42
46
//Starting with Android O, this "id" is not necessarily a long (row number),
43
47
//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 );
48
56
}
49
- final Uri contentUri = ContentUris .withAppendedId (
50
- Uri .parse ("content://downloads/public_downloads" ), Long .valueOf (id ));
51
-
52
- return getDataColumn (context , contentUri , null , null );
53
57
}
54
58
catch (Exception ex ) {
55
59
//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())) {
85
89
if (isGooglePhotosUri (uri ))
86
90
return uri .getLastPathSegment ();
87
91
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
+ }
89
98
}
90
99
// Other Providers
91
100
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 );
112
102
}
113
103
}
114
104
// MediaStore (and general)
@@ -128,14 +118,17 @@ else if ("file".equalsIgnoreCase(uri.getScheme())) {
128
118
return null ;
129
119
}
130
120
121
+ @ Nullable
131
122
private static String getContentName (ContentResolver resolver , Uri uri ) {
132
123
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
+ }
139
132
}
140
133
return null ;
141
134
}
@@ -179,6 +172,52 @@ public static String getDataColumn(Context context, Uri uri, String selection,
179
172
return result ;
180
173
}
181
174
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
+ }
182
221
183
222
/**
184
223
* @param uri The Uri to check.
0 commit comments