2
0
mirror of synced 2025-02-20 01:58:09 +00:00

Issue #572 - Fix for Storage.putFile() with content:// paths

This commit is contained in:
Peter Oxenham 2017-11-02 13:26:09 +10:00
parent 270279551a
commit 7dba72a377

View File

@ -12,8 +12,6 @@ import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
import android.net.Uri; import android.net.Uri;
import android.database.Cursor;
import android.provider.MediaStore;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Arguments;
@ -339,16 +337,12 @@ public class RNFirebaseStorage extends ReactContextBaseJavaModule {
Log.i(TAG, "putFile: " + localPath + " to " + path); Log.i(TAG, "putFile: " + localPath + " to " + path);
try { try {
Uri file; Uri file = getURI(localPath);
if (localPath.startsWith("content://")) { InputStream inputStream = getReactApplicationContext().getContentResolver()
String realPath = getRealPathFromURI(localPath); .openInputStream(file);
file = Uri.fromFile(new File(realPath));
} else {
file = Uri.fromFile(new File(localPath));
}
StorageMetadata md = buildMetadataFromMap(metadata); 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 // register observers to listen for when the download is done or if it fails
uploadTask 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 * @param uri
* @return * @return
*/ */
private String getRealPathFromURI(final String uri) { private Uri getURI(final String uri) {
Cursor cursor = null; Uri parsed = Uri.parse(uri);
try {
String[] proj = {MediaStore.Images.Media.DATA}; if (parsed.getScheme() == null || parsed.getScheme().isEmpty()) {
cursor = getReactApplicationContext().getContentResolver().query(Uri.parse(uri), proj, null, null, null); return Uri.fromFile(new File(uri));
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
} }
return parsed;
} }
/** /**