3
                Uri downloaduri=taskSnapshot.getDownloadUrl();//here i cant use getdownloadurl() function
                DatabaseReference new_prod=db.push();
                new_prod.child("product name").setValue(prod_name);
                new_prod.child("product price").setValue(prod_price);
                new_prod.child("available stock").setValue(prod_quan);
                new_prod.child("product image").setValue(downloaduri);
                pd.dismiss();//fragments code

I was unable to use getdownloadurl .I have already stored image in the firebase storage.Is it the fragment that is restricting the use of getdownloadurl? My motive is to make a query to store in firebase.please help me.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

5 Answers5

11

The taskSnapshot.getDownloadUrl() method was removed in recent versions of the Firebase Storage SDK. You will need to instead get the download URL from the StorageReference now.

Calling StorageReference.getDownloadUrl() returns a Task, since it needs to retrieve the download URL from the server. So you'll need a completion listener to get the actual URL.

From the documentation on downloading a file:

 storageRef.child("users/me/profile.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
        // Got the download URL for 'users/me/profile.png' in uri
        System.out.println(uri.toString());
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle any errors
    }
});

Alternatively that first line could be this if you're getting the download URL right after uploading (as in your case):

taskSnapshot.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
4

This worked for me after hours of research and differents ways :

filepath.putFile(resultUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
     @Override
     public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

           //here
           Task<Uri> urlTask = taskSnapshot.getStorage().getDownloadUrl();
           while (!urlTask.isSuccessful());
           Uri downloadUrl = urlTask.getResult();

           final String sdownload_url = String.valueOf(downloadUrl);
Firanolfind
  • 1,559
  • 2
  • 17
  • 36
Yous Self
  • 41
  • 4
0
storageReference.child("YOUR_CHILD")
    .putFile("FILE")
    .addOnSuccessListener(new OnSuccessListener() {
       @Override
       public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
             taskSnapshot
               .getStorage()
               .getDownloadUrl()
               .addOnSuccessListener(new OnSuccessListener() {
                                @Override
                                public void onSuccess(Uri uri) {
                                    //Put your result here
                                }
                            });

                   }   
JPDroid
  • 46
  • 3
0

Below method worked for me.

    _fbs_upload_success_listener = new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot _param1) {

                Task<Uri> urlTask = _param1.getStorage().getDownloadUrl();
                while (!urlTask.isSuccessful());
                Uri downloadUrl = urlTask.getResult();
                String _downloadUrl = downloadUrl.toString();
   };
kaigalmane
  • 41
  • 7
0

This is Kotlin Solution which worked for me after searching for 3 hrs

 val uploadTask =
        FirebaseStorage.getInstance().reference.child("images").child(imageName).putBytes(data)

    uploadTask.addOnFailureListener {
        // Handle unsuccessful uploads
        Toast.makeText(baseContext, "Failed to upload", Toast.LENGTH_LONG).show()
    }.addOnSuccessListener {
        Toast.makeText(
            baseContext, "Image uploaded",
            Toast.LENGTH_LONG
        ).show()
        var downloadUrl: Uri? = null
        FirebaseStorage.getInstance().reference.child("images")
            .child(imageName).downloadUrl.addOnSuccessListener { it1 ->
            downloadUrl = it1 
          
        }


    }

downloadUrl is your URL

Uday
  • 155
  • 2
  • 6