0

I searched a lot of questions on stack overflow, looking for an answer to this, but none of them are working for me.

I am using a code like following to upload an image from user's device to Firebase storage, and along with that, update the imageURL node in firebase with URL of the image uploaded.

private void uploadFile(Bitmap bitmap) {

        FirebaseStorage storage = FirebaseStorage.getInstance();
        final StorageReference storageRef = storage.getReference();

        final StorageReference ImagesRef = storageRef.child("images/"+mAu.getCurrentUser().getUid()+".jpg");


        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 20, baos);
        byte[] data = baos.toByteArray();
        UploadTask uploadTask = ImagesRef.putBytes(data);



        uploadTask.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                Log.i("whatTheFuck:",exception.toString());
            }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
                // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.



                Task<Uri> downloadUrl = taskSnapshot.getMetadata().getReference().getDownloadUrl();

                DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(mAu.getCurrentUser().getUid());

                Log.i("seeThisUrl", downloadUrl.toString());

                ref.child("imageURL").setValue(downloadUrl.toString());


            }
        });

But the imageURL is always not what I am expecting, it is something of the form:

com.google.android.gms.tasks.zzu@51bc124

instead of actual URL, and I am not able to find an answer to why it's happening like that.

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

1 Answers1

0

You are printing:

com.google.android.gms.tasks.zzu@51bc124

Because this is the address from memory of your downloadUrl object which is of type Task<Uri> and not the actual Uri.

According to the official documentation regarding on how to get a download URL:

After uploading a file, you can get a URL to download the file by calling the getDownloadUrl() method on the StorageReference:

final StorageReference ref = storageRef.child("/* Your path */");
uploadTask = ref.putFile(file);

Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
    @Override
    public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
        if (!task.isSuccessful()) {
            throw task.getException();
        }

        return ref.getDownloadUrl(); //Call the getDownloadUrl() method
    }
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
    @Override
    public void onComplete(@NonNull Task<Uri> task) {
        if (task.isSuccessful()) {
            Uri downloadUri = task.getResult();
        } else {
            // Handle failures
        }
    }
});

This is an example from the docs.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193