@@ -49,11 +49,8 @@ class FirestackStorageModule extends ReactContextBaseJavaModule {
49
49
private static final String FileTypeRegular = "FILETYPE_REGULAR" ;
50
50
private static final String FileTypeDirectory = "FILETYPE_DIRECTORY" ;
51
51
52
- private ReactContext mReactContext ;
53
-
54
52
public FirestackStorageModule (ReactApplicationContext reactContext ) {
55
53
super (reactContext );
56
- mReactContext = reactContext ;
57
54
58
55
Log .d (TAG , "New instance" );
59
56
}
@@ -142,7 +139,13 @@ public void uploadFile(final String urlStr, final String name, final String file
142
139
Log .i (TAG , "From file: " + filepath + " to " + urlStr + " with name " + name );
143
140
144
141
try {
145
- Uri file = Uri .fromFile (new File (filepath ));
142
+ Uri file ;
143
+ if (filepath .startsWith ("content://" )) {
144
+ String realPath = getRealPathFromURI (filepath );
145
+ file = Uri .fromFile (new File (realPath ));
146
+ } else {
147
+ file = Uri .fromFile (new File (filepath ));
148
+ }
146
149
147
150
StorageMetadata .Builder metadataBuilder = new StorageMetadata .Builder ();
148
151
Map <String , Object > m = FirestackUtils .recursivelyDeconstructReadableMap (metadata );
@@ -190,7 +193,7 @@ public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
190
193
WritableMap data = Arguments .createMap ();
191
194
data .putString ("eventName" , "upload_progress" );
192
195
data .putDouble ("progress" , progress );
193
- FirestackUtils .sendEvent (mReactContext , "upload_progress" , data );
196
+ FirestackUtils .sendEvent (getReactApplicationContext () , "upload_progress" , data );
194
197
}
195
198
}
196
199
})
@@ -203,7 +206,7 @@ public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
203
206
WritableMap data = Arguments .createMap ();
204
207
data .putString ("eventName" , "upload_paused" );
205
208
data .putString ("ref" , bucket );
206
- FirestackUtils .sendEvent (mReactContext , "upload_paused" , data );
209
+ FirestackUtils .sendEvent (getReactApplicationContext () , "upload_paused" , data );
207
210
}
208
211
});
209
212
} catch (Exception ex ) {
@@ -214,21 +217,29 @@ public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
214
217
@ ReactMethod
215
218
public void getRealPathFromURI (final String uri , final Callback callback ) {
216
219
try {
217
- Context context = getReactApplicationContext ();
218
- String [] proj = {MediaStore .Images .Media .DATA };
219
- Cursor cursor = context .getContentResolver ().query (Uri .parse (uri ), proj , null , null , null );
220
- int column_index = cursor .getColumnIndexOrThrow (MediaStore .Images .Media .DATA );
221
- cursor .moveToFirst ();
222
- String path = cursor .getString (column_index );
223
- cursor .close ();
224
-
220
+ String path = getRealPathFromURI (uri );
225
221
callback .invoke (null , path );
226
222
} catch (Exception ex ) {
227
223
ex .printStackTrace ();
228
224
callback .invoke (makeErrorPayload (1 , ex ));
229
225
}
230
226
}
231
227
228
+ private String getRealPathFromURI (final String uri ) {
229
+ Cursor cursor = null ;
230
+ try {
231
+ String [] proj = {MediaStore .Images .Media .DATA };
232
+ cursor = getReactApplicationContext ().getContentResolver ().query (Uri .parse (uri ), proj , null , null , null );
233
+ int column_index = cursor .getColumnIndexOrThrow (MediaStore .Images .Media .DATA );
234
+ cursor .moveToFirst ();
235
+ return cursor .getString (column_index );
236
+ } finally {
237
+ if (cursor != null ) {
238
+ cursor .close ();
239
+ }
240
+ }
241
+ }
242
+
232
243
private WritableMap getDownloadData (final UploadTask .TaskSnapshot taskSnapshot ) {
233
244
Uri downloadUrl = taskSnapshot .getDownloadUrl ();
234
245
StorageMetadata d = taskSnapshot .getMetadata ();
0 commit comments