0

How to get the download URI?

String downloadThumbUri = uploadTask.getResult().getStorage().getDownloadUrl().toString();

is not working.

I am using Bitmap for compressing an Image to use it as a thumbnail. Here's the code:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
compressedImageFile.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] thumbData = baos.toByteArray();

final UploadTask uploadTask = storageReference.child("post_images/thumbs").child(randomName + ".jpg").putBytes(thumbData);

uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

        //Toast.makeText(NewPostActivity.this,"Thumbnail was uploaded ",Toast.LENGTH_LONG).show();

        String downloadThumbUri = uploadTask.getResult().getStorage().getDownloadUrl().toString();

        Map<String,Object> postMap = new HashMap<>();

        postMap.put("image url",downloadUri);
        postMap.put("thumb",downloadThumbUri);
        postMap.put("desc",desc);
        postMap.put("user_id",current_user_id);
        postMap.put("timestamp",FieldValue.serverTimestamp());

        firebaseFirestore.collection("Posts").add(postMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
            @Override
            public void onComplete(@NonNull Task<DocumentReference> task) {

                if(task.isSuccessful()){

                    newPostProgress.setVisibility(View.INVISIBLE);
                    Toast.makeText(NewPostActivity.this,"Post was added ",Toast.LENGTH_LONG).show();
                    Intent mainIntent = new Intent(NewPostActivity.this,MainActivity.class);
                    startActivity(mainIntent);
                    finish();

                }
            }
        })
    }
}
MarianD
  • 13,096
  • 12
  • 42
  • 54
Hitesh
  • 3
  • 1
  • `getDownloadUrl` is an asynchronous operation, which returns a `Task`. You need to add a `OnSuccessListener` to that task too, to get the actual download URL. For an example see the link Alex provided, but also the [documentation on downloading files](https://firebase.google.com/docs/storage/android/download-files#download_data_via_url), and the (slightly more complex example in the) [documentation on uploading files](https://firebase.google.com/docs/storage/android/upload-files#get_a_download_url) – Frank van Puffelen Sep 12 '19 at 13:30

1 Answers1

2

Use the following code to get the download uri and store in Firestore. Put it inside onSucess

uploadTask.getDownloadUrl().addOnCompleteListener(new OnCompleteListener<Uri>() {
                @Override
                public void onComplete(@NonNull Task<Uri> task) {
                    String downloadThumbUri = task.getResult().toString();
                    Map<String,Object> postMap = new HashMap<>();
                    postMap.put("image url",downloadUri);
                    postMap.put("thumb",downloadThumbUri);
                    postMap.put("desc",desc);
                    postMap.put("user_id",current_user_id);
                    postMap.put("timestamp",FieldValue.serverTimestamp());

                    firebaseFirestore.collection("Posts").add(postMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
                        @Override
                        public void onComplete(@NonNull Task<DocumentReference> task) {

                            if(task.isSuccessful()){

                                newPostProgress.setVisibility(View.INVISIBLE);
                                Toast.makeText(NewPostActivity.this,"Post was added ",Toast.LENGTH_LONG).show();
                                Intent mainIntent = new Intent(NewPostActivity.this,MainActivity.class);
                                startActivity(mainIntent);
                                finish();
                            }
            });
        }
    })
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            progressBar.setVisibility(View.GONE);
            Toast.makeText(ProfileActivity.this, "aaa "+e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
Ashish
  • 6,791
  • 3
  • 26
  • 48