0

I'm trying to get the download url of the image uploaded to firebase database. But Task Uri imageURL = storageReference.getDownloadUrl(); doesn't gives the actual download URL of an image stored in the firebase storage i.e it gives this - com.google.android.gms.tasks.zzu@27da5837

getdownloadUrl() doesn't work in case of as it is deprecated: Uri imageUrl = storagereference.getdownloadUrl(); //Gives error

please see the code here:

final StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("images").child(imageName);

    final UploadTask uploadTask = storageReference.putBytes(data);

    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle unsuccessful uploads
            Toast.makeText(CreateSnapActivity.this, "Upload Failed", Toast.LENGTH_SHORT).show();
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
            // taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc.

            //Task<Uri> imageURL = storageReference.getDownloadUrl();  
            //Log.i("URL", imageURL.toString());        // gives this url - com.google.android.gms.tasks.zzu@27da5837 which is not correct


            Task<Uri> urlTask = storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                Log.i("Url", String.valueOf(uri));
                imageUrl = uri.toString();  //Gives the correct url but it cannot store this uri value outside this function i.e for Intent shown below outside this func.
               
                }
            });

            Toast.makeText(CreateSnapActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(CreateSnapActivity.this, ChooseUserActivity.class);
            intent.putExtra("imageName", imageName);
            intent.putExtra("imageURL", imageUrl);  
            intent.putExtra("message", captionEditText.getText().toString());
            startActivity(intent);

        }
    });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Aditya
  • 3
  • 2
  • " //Gives error" -> what is the exact error you get? If it's an exception also include the complete stack trace. You can add both to your question, by clicking the `edit` link under it. – Frank van Puffelen Jul 11 '20 at 15:01

2 Answers2

0

imageURL is a task.You are trying to print a task. not task's result. please try this code:

Task<Uri> imageURL = storageReference.getDownloadUrl();  
Log.i("URL", imageURL.toString());
Log.i("URL", imageURL.result.toString());
sabrey
  • 47
  • 1
  • 7
0

Calling getDownloadUrl() starts an asynchronous operation, which may take some time to complete. Because of this, your main code continues to run while the download URL is being retrieved to prevent blocking the user. Then when the download URL is available, your onSuccess gets called. Because of this, by the time your intent.putExtra("imageURL", imageUrl) now runs, the imageUrl = uri.toString() hasn't run yet.

To see that this indeed true, I recommend putting some logging in the code to just show the flow, or running it in a debugger and setting breakpoints on the lines I indicated above.

To fix it, any code that needs the download URL, needs to be inside the onSuccess, or be called from there. This applies not just here, but to all code that runs asynchronously, which includes most modern cloud APIs. So I recommend spending some time now studying this behavior, so that you're more comfortable with it going forward.

In your code:

final StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("images").child(imageName);

final UploadTask uploadTask = storageReference.putBytes(data);

uploadTask.addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        Toast.makeText(CreateSnapActivity.this, "Upload Failed", Toast.LENGTH_SHORT).show();
    }
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
        Task<Uri> urlTask = storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                imageUrl = uri.toString();
                Toast.makeText(CreateSnapActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(CreateSnapActivity.this, ChooseUserActivity.class);
                intent.putExtra("imageName", imageName);
                intent.putExtra("imageURL", imageUrl);  
                intent.putExtra("message", captionEditText.getText().toString());
                startActivity(intent);
           
            }
        });
    }
});

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you so much! The problem resolved. I should have put the intent code inside onSuccess method. – Aditya Jul 11 '20 at 15:39