Skip to content

Commit

Permalink
Issue #572 - Fix for Storage.putFile() with content:// paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Oxenham committed Nov 2, 2017
1 parent 2702795 commit 7dba72a
Showing 1 changed file with 11 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
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;
Expand Down Expand Up @@ -339,16 +337,12 @@ public void putFile(final String appName, final String path, final String localP
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
Expand Down Expand Up @@ -415,24 +409,18 @@ private StorageReference getReference(String path, String appName) {
}

/**
* 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;
}

/**
Expand Down

0 comments on commit 7dba72a

Please sign in to comment.