@@ -49,11 +49,8 @@ class FirestackStorageModule extends ReactContextBaseJavaModule {
4949 private static final String FileTypeRegular = "FILETYPE_REGULAR" ;
5050 private static final String FileTypeDirectory = "FILETYPE_DIRECTORY" ;
5151
52- private ReactContext mReactContext ;
53-
5452 public FirestackStorageModule (ReactApplicationContext reactContext ) {
5553 super (reactContext );
56- mReactContext = reactContext ;
5754
5855 Log .d (TAG , "New instance" );
5956 }
@@ -142,7 +139,13 @@ public void uploadFile(final String urlStr, final String name, final String file
142139 Log .i (TAG , "From file: " + filepath + " to " + urlStr + " with name " + name );
143140
144141 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+ }
146149
147150 StorageMetadata .Builder metadataBuilder = new StorageMetadata .Builder ();
148151 Map <String , Object > m = FirestackUtils .recursivelyDeconstructReadableMap (metadata );
@@ -190,7 +193,7 @@ public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
190193 WritableMap data = Arguments .createMap ();
191194 data .putString ("eventName" , "upload_progress" );
192195 data .putDouble ("progress" , progress );
193- FirestackUtils .sendEvent (mReactContext , "upload_progress" , data );
196+ FirestackUtils .sendEvent (getReactApplicationContext () , "upload_progress" , data );
194197 }
195198 }
196199 })
@@ -203,7 +206,7 @@ public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
203206 WritableMap data = Arguments .createMap ();
204207 data .putString ("eventName" , "upload_paused" );
205208 data .putString ("ref" , bucket );
206- FirestackUtils .sendEvent (mReactContext , "upload_paused" , data );
209+ FirestackUtils .sendEvent (getReactApplicationContext () , "upload_paused" , data );
207210 }
208211 });
209212 } catch (Exception ex ) {
@@ -214,21 +217,29 @@ public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
214217 @ ReactMethod
215218 public void getRealPathFromURI (final String uri , final Callback callback ) {
216219 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 );
225221 callback .invoke (null , path );
226222 } catch (Exception ex ) {
227223 ex .printStackTrace ();
228224 callback .invoke (makeErrorPayload (1 , ex ));
229225 }
230226 }
231227
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+
232243 private WritableMap getDownloadData (final UploadTask .TaskSnapshot taskSnapshot ) {
233244 Uri downloadUrl = taskSnapshot .getDownloadUrl ();
234245 StorageMetadata d = taskSnapshot .getMetadata ();
0 commit comments