0

I'm currently working on java android application, and I have problem with showing uploaded images in app. I'm uploading images on firebase storage, and realtime database. This is screenshot of realtime database item:

this is my item in realtime database

when i change mImageUrl value to firebase storage access token my images are shown just fine.

I think i need to change only this part of code:

 Upload upload = new Upload(productName.getText().toString().trim(), productDesc.getText().toString().trim(), taskSnapshot.getMetadata().
                        getReference().getDownloadUrl().toString());

But here is whole method for uploading files to firebase:

private void uploadFile() {

    if(mImageUri != null){
        
        StorageReference fileReference = mStorageRef.child(System.currentTimeMillis() + "." + getFileExtension(mImageUri));

        mUploadTask = fileReference.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
              
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        uploadProgress.setProgress(0);
                    }
                }, 1000);

                Toast.makeText(AddItemForSale.this, "Upload successful", Toast.LENGTH_LONG).show();
                Upload upload = new Upload(productName.getText().toString().trim(), productDesc.getText().toString().trim(), taskSnapshot.getMetadata().
                        getReference().getDownloadUrl().toString());

                String uploadId = mDatabaseRef.push().getKey();
                mDatabaseRef.child(uploadId).setValue(upload);
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
              Toast.makeText(AddItemForSale.this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onProgress(@NonNull UploadTask.TaskSnapshot snapshot) {
               double progress = (100.0 * snapshot.getBytesTransferred() / snapshot.getTotalByteCount());
               uploadProgress.setProgress((int) progress);
            }
        });
    }else {
        Toast.makeText(this, "No file selected", Toast.LENGTH_SHORT).show();
    }
}
Ranko Koturic
  • 127
  • 1
  • 1
  • 8
  • 1
    `getReference().getDownloadUrl().toString()` That's not how you can get the download URL. Please check the [duplicate](https://stackoverflow.com/questions/53299915/how-to-get-the-download-url-from-firebase-storage) to see how you can solve this. – Alex Mamo Oct 11 '21 at 11:27
  • 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 Oct 11 '21 at 14:00
  • 1
    Thanks a lot Alex and Frank. @FrankvanPuffelen i found solution in answer you provided. – Ranko Koturic Oct 12 '21 at 07:10

0 Answers0