0

I know there are several ways to replace getDownloadUrl() but it never works for me. Please help me if someone knows good method

So I'm trying to upload some postings with photos. Other things show up except for the images that I uploaded. And I think getDownloadUrl() is the problem.

 public void onClick(View v) {
        if (v.getId() == R.id.addphoto_btn_upload && photoUrl != null) {

            Log.d("AddPhotoActivity", " upload button clicked");

            File myFile = new File(photoUrl);
            Uri contentUri = Uri.fromFile(myFile);

            StorageReference storageReference = firebaseStorage.getReferenceFromUrl("gs://mystagram.appspot.com/").child("images").child(contentUri.getLastPathSegment());

            UploadTask uploadTask = storageReference.putFile(contentUri);
            uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Toast.makeText(AddPhotoActivity.this, getString(R.string.upload_success), Toast.LENGTH_SHORT).show();




                    Task<Uri> uri = taskSnapshot.getStorage().getDownloadUrl();
 


                  
                    DatabaseReference images = firebaseDatabase.getReference().child("images").push();

                    //Time
                    Date date = new Date();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                    ContentDTO contentDTO = new ContentDTO();


                   
                    contentDTO.imageUrl = uri.toString();

                    contentDTO.explain = explain_editText.getText().toString();
                    
                    contentDTO.userId = firebaseAuth.getCurrentUser().getEmail();
                    
                    contentDTO.timeStamp = Long.valueOf(simpleDateFormat.format(date));

                    images.setValue(contentDTO);
                    AddPhotoActivity.this.finish();

                }
            });

How can I change this?

Ally
  • 11
  • 1

1 Answers1

0

Task uri = taskSnapshot.getStorage().getDownloadUrl();

This will not provide you download able link for that purpose you have to use addOnCompleteListener that will provide you Task<UploadTask.TaskSnapshot> task through you can get your file link.

A running example is here:

    public void uploadImageSavePost(Bitmap bitmap) {
        StorageReference ref = FirebaseStorage.getInstance().getReference();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 20, baos);
        byte[] data = baos.toByteArray();
        Long tsLong = System.currentTimeMillis() / 1000;
        String ts = tsLong.toString();
        final UploadTask uploadTask = ref.child("images/" + ts + ".jpg").putBytes(data);
        uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
            if (task.isSuccessful()) {
                task.getResult().getMetadata().getReference().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        img = uri.toString(); // here it is the link of your file.
                        savePost();
                    }
                });
            }
        }
    });
}
Zeeshan Ali
  • 667
  • 8
  • 16