0

In my case, I tried with the following code on Android Java but I am unable to get the download URL for imageURL; Can you please help me to get the download URL in firebase?

ActivityResultLauncher<String> mGetContent = registerForActivityResult(new ActivityResultContracts.GetContent(),
        new ActivityResultCallback<Uri>() {
            @Override
            public void onActivityResult(Uri uri) {
                //You are provided with uri of the image . Take this uri and assign it to Picasso
                //Intent openGalleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                mImageUri = uri.normalizeScheme();
                Picasso.get().load(mImageUri).fit().into(productImage);
            }
        });

upload File code

private void uploadFile(){
  if(mImageUri != null){
      StorageReference fileReference = storageReference.child(System.currentTimeMillis()+ "."+ getFileExtension(mImageUri));
      storageTask = fileReference.putFile(mImageUri)
              .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                  @Override
                  public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                      Toast.makeText(Add_Product.this,"Product Added Successfully",Toast.LENGTH_LONG).show();
                      AdminProductModel addProduct = new AdminProductModel(productName.getText().toString().trim(),
                              //fileReference.getDownloadUrl().toString(),
                              //taskSnapshot.getMetadata().getReference().getDownloadUrl().toString(),
                              taskSnapshot.getUploadSessionUri().toString(),
                              unitPrice.getText().toString().trim(),
                              description.getText().toString().trim(),
                              liter.getText().toString().trim());
                      String uploadId = databaseReference.push().getKey();
                      databaseReference.child(uploadId).setValue(addProduct);

                  }
              })

I used these ways to get the URL but doesn't work

taskSnapshot.getMetadata().getReference().getDownloadUrl().toString()
fileReference.getDownloadUrl().toString()
taskSnapshot.getStorage().getDownloadUrl().toString()

Firebase Image

Nusry KR
  • 85
  • 1
  • 12
  • 1
    Determining the download URL requires a call to the server. Because of this, the call to `getDownloadUrl()` returns a `Task` that completes when the download URL comes back from the server. You'll need to call `addSuccessListener()` on it to wait for it to complete. See the documentation [here](https://firebase.google.com/docs/storage/android/download-files#download_data_via_url) and this [answer](https://stackoverflow.com/a/51064689) – Frank van Puffelen Aug 31 '21 at 13:32
  • @FrankvanPuffelen Thanks now it's working – Nusry KR Sep 01 '21 at 16:24

0 Answers0