1

I have this code which uploads an image from imageview on the click of a button. Even if the image gets uploaded by this code, I'm unable to get the download url for the image which I can use in future reference purposes. Kindly help me in getting the download URL. FYI, the getDownloadURl() function has been deprecated and is not working. Thank you!

    Button uploadBtn = findViewById(R.id.upload_btn);
            uploadBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    FirebaseStorage storage = FirebaseStorage.getInstance();
                    // Create a storage reference from our app
                    StorageReference storageRef = storage.getReferenceFromUrl("gs://'''''''.appspot.com/");
    
    // Create a reference to "mountains.jpg"
                    StorageReference mountainsRef = storageRef.child(System.currentTimeMillis() + ".jpg");
    
    // Create a reference to 'images/mountains.jpg'
                    StorageReference mountainImagesRef = storageRef.child("images" + System.currentTimeMillis() + ".jpg");

    // While the file names are the same, the references point to different files
                    mountainsRef.getName().equals(mountainImagesRef.getName());    // true
                    mountainsRef.getPath().equals(mountainImagesRef.getPath());    // false

                imageView.setDrawingCacheEnabled(true);
                imageView.buildDrawingCache();
                Bitmap bitmap = imageView.getDrawingCache();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                byte[] data = baos.toByteArray();

                UploadTask uploadTask = mountainsRef.putBytes(data);
                uploadTask.addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {
                        // Handle unsuccessful uploads
                    }
                }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
                        String downloadUrl = taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();
                        Toast.makeText(ClickImage.this, downloadUrl, Toast.LENGTH_SHORT).show();

                    }

                });


            }
        });
MohammadAmin Mohammadi
  • 1,183
  • 3
  • 14
  • 23
Ankur Paul
  • 13
  • 1
  • 5
  • 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 Jul 13 '20 at 14:54

1 Answers1

0

I got this problem with me before, who gets to download Firebase correctly but not the link in downloadUrl.

After what I searched for the matter, there is a thing called AsyncTask and its method of work works to override the orders that need a greater period of time and executes the orders after it and when it is finished it returns to it

Solve this problem You can use this method to retrieve the value of downloadUrl

                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                    storageReference.getDownloadUrl().addOnCompleteListener(task -> {

                        String downloadUrl = Objects.requireNonNull(task.getResult()).toString();
    
                    });

I hope I simplified things for you and help you .

Adel B-Lahlouh
  • 1,059
  • 1
  • 8
  • 20