TAGS :Viewed: 12 - Published at: a few seconds ago

[ Convert uri to string with firebase storage ]

I get a simple example rom firebase how to upload image to storage. Upload fine, and when onSucess i have uri to download something like this:

https://firebasestorage.googleapis.com/v0/b/sexstartup-e3a65.appspot.com/o/User%3A%20epAPhTPZtUQwNprZpUIKOOc4DJk2%2Fimg.jpg?alt=media&token=49a479c2-fdfa-42ec-9376-278fca3b73e3

When i try to convert this uri to string i have Nullpointerexception, wat's wrong?

 Uri downloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
 String string_dwload = downloadUrl.toString();//valueOf not work too 

Answer 1


You need to change your code by calling method getDownloadUrl() on the taskSnapshot object (instead of getMetadata()) - so your code in the onSuccess callback should look like this:

public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        //get the download URL like this:
        Uri downloadUrl = taskSnapshot.getDownloadUrl();
        //and you can convert it to string like this:
        String string_dwload = downloadUrl.toString();        
    }

I hope this helps. By the way, please check the article/tutorial on Uploading file on Android