0

I am trying to retrieve an image from my firebase storage but I'm having problems storing the image download url. I am using final String downloadUrl = task.getResult().getMetadata().getReference().getDownloadUrl().toString(); but this is only reutrning a reference to the task I believe - com.google.android.gms.tasks.zzu@add13a0

What I want to be storing/retrieving is https://firebasestorage.googleapis.com/v0/b/chat-poll-application.appspot.com/o/Group%20Images%2FGig.jpg?alt=media&token=9a90fc94-e65f-4ace-9259-aaaa91b449c3

Can anyone see where I'm going wrong?

Here is the full code:

filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(GroupSettingsActivity.this, "Group image uploaded successfully", Toast.LENGTH_SHORT).show();
                            final String downloadUrl = task.getResult().getMetadata().getReference().getDownloadUrl().toString();
                            RootRef.child("Groups").child(currentGroupName).child("Image").setValue(downloadUrl)
                                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task) {
                                            if (task.isSuccessful()) {
                                                Toast.makeText(GroupSettingsActivity.this, "Image saved successfully", Toast.LENGTH_SHORT).show();
                                                loadingBar.dismiss();
                                            } else {
                                                String message = task.getException().toString();
                                                Toast.makeText(GroupSettingsActivity.this, "Error: " + message, Toast.LENGTH_SHORT).toString();
                                                loadingBar.dismiss();
                                            }
                                        }
                                    });
                        } else {
                            String message = task.getException().toString();
                            Toast.makeText(GroupSettingsActivity.this, "Error " + message, Toast.LENGTH_SHORT).show();
                            loadingBar.dismiss();
                        }
                    }
                });

Here is my database:

enter image description here

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Joe Leeson
  • 71
  • 11
  • This does not work: `getMetadata().getReference().getDownloadUrl().toString();`. The call to `getDownloadUrl` returns a `Task` and you're converting that `Task` to a string. To get the actual download URL you need to add a completion listener to the task as shown here: https://stackoverflow.com/questions/51056397/how-to-use-getdownloadurl-in-recent-versions – Frank van Puffelen Mar 29 '20 at 15:53

1 Answers1

1

Declare this object globally

Uri uri;

Set uri when user fetches the Image from the gallery

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {

        uri = data.getData();
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            jimg.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Here sUrl will have downloadURL

private void uploadImage() {
    try {
        final StorageReference ref = storageReference.child(  UUID.randomUUID().toString() );

        //uploads the image
        ref.putFile( uri ).addOnSuccessListener( new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                ref.getDownloadUrl().addOnSuccessListener( new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        final Uri downloadUrl = uri;
                        sUrl = downloadUrl.toString();

                    }
                } ).addOnFailureListener( new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {

                    }
                } );


            }
        } );
    }catch (Exception e){}
}