From 7dba72a3775223ec4da89ef5616bfd17e23fc083 Mon Sep 17 00:00:00 2001 From: Peter Oxenham Date: Thu, 2 Nov 2017 13:26:09 +1000 Subject: [PATCH 1/2] Issue #572 - Fix for Storage.putFile() with content:// paths --- .../firebase/storage/RNFirebaseStorage.java | 34 ++++++------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/android/src/main/java/io/invertase/firebase/storage/RNFirebaseStorage.java b/android/src/main/java/io/invertase/firebase/storage/RNFirebaseStorage.java index 5e4386fd..fce5a2f5 100644 --- a/android/src/main/java/io/invertase/firebase/storage/RNFirebaseStorage.java +++ b/android/src/main/java/io/invertase/firebase/storage/RNFirebaseStorage.java @@ -12,8 +12,6 @@ import java.util.Map; import java.util.HashMap; import android.net.Uri; -import android.database.Cursor; -import android.provider.MediaStore; import android.support.annotation.NonNull; import com.facebook.react.bridge.Arguments; @@ -339,16 +337,12 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule { Log.i(TAG, "putFile: " + localPath + " to " + path); try { - Uri file; - if (localPath.startsWith("content://")) { - String realPath = getRealPathFromURI(localPath); - file = Uri.fromFile(new File(realPath)); - } else { - file = Uri.fromFile(new File(localPath)); - } + Uri file = getURI(localPath); + InputStream inputStream = getReactApplicationContext().getContentResolver() + .openInputStream(file); StorageMetadata md = buildMetadataFromMap(metadata); - UploadTask uploadTask = reference.putFile(file, md); + UploadTask uploadTask = reference.putStream(inputStream, md); // register observers to listen for when the download is done or if it fails uploadTask @@ -415,24 +409,18 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule { } /** - * Internal helper to convert content:// uri's to a real path + * Create a Uri from the path, defaulting to file when there is no supplied scheme * * @param uri * @return */ - private String getRealPathFromURI(final String uri) { - Cursor cursor = null; - try { - String[] proj = {MediaStore.Images.Media.DATA}; - cursor = getReactApplicationContext().getContentResolver().query(Uri.parse(uri), proj, null, null, null); - int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); - cursor.moveToFirst(); - return cursor.getString(column_index); - } finally { - if (cursor != null) { - cursor.close(); - } + private Uri getURI(final String uri) { + Uri parsed = Uri.parse(uri); + + if (parsed.getScheme() == null || parsed.getScheme().isEmpty()) { + return Uri.fromFile(new File(uri)); } + return parsed; } /** From cf934908a3f86bc021c9d78b3f8c3b2ea0c81347 Mon Sep 17 00:00:00 2001 From: Peter Oxenham Date: Fri, 3 Nov 2017 09:44:04 +1000 Subject: [PATCH 2/2] Issue #572 - Refactored to get upload progress to report total bytes correctly --- .../io/invertase/firebase/storage/RNFirebaseStorage.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/android/src/main/java/io/invertase/firebase/storage/RNFirebaseStorage.java b/android/src/main/java/io/invertase/firebase/storage/RNFirebaseStorage.java index fce5a2f5..209e832f 100644 --- a/android/src/main/java/io/invertase/firebase/storage/RNFirebaseStorage.java +++ b/android/src/main/java/io/invertase/firebase/storage/RNFirebaseStorage.java @@ -338,11 +338,8 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule { try { Uri file = getURI(localPath); - InputStream inputStream = getReactApplicationContext().getContentResolver() - .openInputStream(file); - StorageMetadata md = buildMetadataFromMap(metadata); - UploadTask uploadTask = reference.putStream(inputStream, md); + UploadTask uploadTask = reference.putFile(file, md); // register observers to listen for when the download is done or if it fails uploadTask