2

I want to fetch Url of the Image that I have uploaded in the Firebase. To fetch the Image I need to have the Url of the specific Image. But there's is problem occurring in the fetching. The Output box shows the following:

error: incompatible types: Task cannot be converted to Uri

If I use Task<Uri> the image url become different. As compare to Uri.

  private void startPosting() {
    if (filePath != null) {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("Uploading...");
        progressDialog.show();

        final StorageReference ref = mStorage.child("Quote_Image" + UUID.randomUUID().toString());
        ref.putFile(filePath)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        progressDialog.dismiss();
                        Toast.makeText(adminUpdateQuotesSend.this , "Uploaded" , Toast.LENGTH_SHORT).show();
                        mDatbase = FirebaseDatabase.getInstance().getReference().child("Quotes");
                        DatabaseReference newPost = mDatbase.push();
                        Uri down=ref.getDownloadUrl();
                            Uri dow=down;
                        newPost.child("ImageUrl").setValue(ref.getDownloadUrl());
                       // newPost.child("Positivity").setValue(PositiveDesc);
                    }
                })
                .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                        double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot
                                .getTotalByteCount());
                        progressDialog.setMessage("Uploaded " + (int) progress + "%");

                    }
                });

    }
}

The error is flashing on this line of code:

Uri down=ref.getDownloadUrl();
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
itz Prateek
  • 101
  • 1
  • 13
  • 1
    `getDownloadUrl` returns a task, which then gives you the download URL once it completes. For a code sample of how to do this, see https://stackoverflow.com/questions/51056397/how-to-use-getdownloadurl-in-recent-versions/51064689#51064689 and the longer: https://firebase.google.com/docs/storage/android/upload-files#get_a_download_url – Frank van Puffelen Nov 15 '18 at 14:06

2 Answers2

1

You are getting the following error:

incompatible types: Task cannot be converted to Uri

Because ref.getDownloadUrl() return a Task object and not a Uri object. There is no way in Java to cast a Task object to a Uri object.

In order to get the download url, you need to use addOnSuccessListener, like in the following lines of code:

ref.putFile(filePath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                String url = uri.toString();

                //Do what you need to do with url
            }
        });
    }
});
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
1

Make sure if you are using a task to check for it if isSuccessful()

To get the download URL of the file you are uploading, you should run this inside a task like this one.

First, we do a reference where to store your file, then we run a task waiting for the result, if the result is true, in this case, we return the downloadUrl of the file, then when we .addOnCompleteListener , that's where we know we already have uploaded the file and have the URL, then we just check again if the upload process has been done correctly and we fetch the download URL of that file with

Uri downloadUrl = task.getResult(); , since it's a URI, we can convert this url into an string like this String downloadUrl = task.getResult().toString(); or simply downloadUrl.toString();

final StorageReference ref = mStorage.child("Quote_Image" + UUID.randomUUID().toString());
    ref.putFile(filePath).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();
    }
    }).addOnCompleteListener(new OnCompleteListener<Uri>() {
        @Override
        public void onComplete(@NonNull Task<Uri> task) {
            if (task.isSuccessful()) {
                 progressDialog.dismiss();
                    Toast.makeText(adminUpdateQuotesSend.this , "Uploaded" , Toast.LENGTH_SHORT).show();
                    mDatbase = FirebaseDatabase.getInstance().getReference().child("Quotes");
                    DatabaseReference newPost = mDatbase.push();
                     Uri downloadUrl = task.getResult();

                    newPost.child("ImageUrl").setValue(downloadUrl.toString());
                   // newPost.child("Positivity").setValue(PositiveDesc);

            } else {
                Toast.makeText(mContext, "upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    });

The only thing is that .addOnProgressListener won't work here.

halfer
  • 19,824
  • 17
  • 99
  • 186
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77