D.Rotenberg
D.Rotenberg

Reputation: 71

Android FireBase error when uploading picture

I am trying to upload a picture via a local file method.

UploadTask uploadTask = currentPicRef.putFile(file, metadata);

When the user pick's image from the gallery or taking a picture via the camera the picture is saved in external storage and I save the uri in shared preference.

I successfully loaded the image into an imageview using the setImageURI(uri) method, but When I call the firebase method and using the same uri (Uri file = Uri.fromFile(new File(fileName));)
I get the error

could not locate file for uploading:file:///content%3A/media/external/images/media/22943

but when I use log to check the local file I get

uri is content://media/external/images/media/22943

also worth to mention that when I used uri.parse() instead uri.fromFile() in firebase to upload from a local file it uploaded the metadata but not the photo itself.

Any ideas how to fix it?

Upvotes: 2

Views: 3473

Answers (2)

Mangi Morobe
Mangi Morobe

Reputation: 1143

fileName can be determined fileUri.toString(). This worked for me:

Uri uploadUri = Uri.fromFile(new File(fileUri.toString()));

Here's an example that works:

// [START upload_from_uri]
private void uploadFromUri(Uri fileUri) {

    Uri uploadUri = Uri.fromFile(new File(fileUri.toString()));

    Log.d(TAG, "uploadFromUri:src:" + fileUri.toString());

    // [START get_child_ref]
    // Get a reference to store file at photos/<FILENAME>.jpg
    final StorageReference photoRef = mStorageRef.child("photos").child(uploadUri.getLastPathSegment());
    // [END get_child_ref]

    // Upload file to Firebase Storage
    // [START_EXCLUDE]
    showProgressDialog();
    // [END_EXCLUDE]
    Log.d(TAG, "uploadFromUri:dst:" + photoRef.getPath());

    photoRef.putFile(uploadUri)
            .addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    // Upload succeeded
                    Log.d(TAG, "uploadFromUri:onSuccess");

                    // Get the public download URL
                    mDownloadUrl = taskSnapshot.getMetadata().getDownloadUrl();

                    // [START_EXCLUDE]
                    hideProgressDialog();
                    updateUI(mAuth.getCurrentUser());
                    // [END_EXCLUDE]
                }
            })
            .addOnFailureListener(this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    // Upload failed
                    Log.w(TAG, "uploadFromUri:onFailure", exception);

                    mDownloadUrl = null;

                    // [START_EXCLUDE]
                    hideProgressDialog();
                    Toast.makeText(MainActivity.this, "Error: upload failed",
                            Toast.LENGTH_SHORT).show();
                    updateUI(mAuth.getCurrentUser());
                    // [END_EXCLUDE]
                }
            });
}
// [END upload_from_uri]

Upvotes: 0

Valentino
Valentino

Reputation: 2135

Maybe it's a bit late, anyway:

could not locate file for uploading:file:///content%3A/media/external/images/media/22943

I think you may have a problem of url encoding (see %3A in the file uri).

Try to decode the uri Uri file = Uri.fromFile(new File(fileName)); before passing it to Firebase Storage Reference.

If it does not work, instead retrieving the image uri in this way Uri.fromFile(new File(fileName)) , you can try to save the local image path in the shared preferences (I'm supposing that when you get the image from gallery or camera, you can have also the local path), and use this to upload the image in the storage Firebase.

Hope can help.

Upvotes: 2

Related Questions