Home » Android » android – Unable to get URI from storage reference on Firebase

android – Unable to get URI from storage reference on Firebase

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m trying to get an image URI that is stored in Firebase storage, in order to process it using another method.
I’m using the following:

    FirebaseStorage storage = FirebaseStorage.getInstance();
    StorageReference storageRef = storage.getReferenceFromUrl(this.getString(R.string.storage_path));
    Uri uri = storageRef.child("groups/pizza.png").getDownloadUrl().getResult();

and getting an error “java.lang.IllegalStateException: Task is not yet complete”

How to&Answers:

You can get the download URL for a file with:

storageRef.child("groups/pizza.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
        // TODO: handle uri
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle any errors
    }
});

See the Firebase documentation for downloading data via a URL.