3

I am retrieving the download URL from a song file that is stored in firebase storage.The URL that i am getting is not the same URL that is in the firebase storage.

Here is the wrong link i get: com.google.android.gms.tasks.zzu@75f559a

Here is the correct link: https://firebasestorage.googleapis.com/v0/b/fouronesixsound-51999.appspot.com/o/Uploads%2F1221?alt=media&token=56beacd5-9abd-4a74-b294-69eb111fcb00

Here is a link to a picture of my database setup: https://i.stack.imgur.com/DvYcw.jpg

This is my code:

  final String fileName = songUri.getLastPathSegment() + "";
    //final String fileName1=songUri.getLastPathSegment()+"";

    final StorageReference storageRef = storage.getReference();


    storageRef.child("Uploads").child(fileName).putFile(songUri)
            .addOnSuccessListener(new 
 OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                    String url = 
storageRef.child("Uploads").child(fileName).getDownloadUrl().toString(); 
//returns the url of the uploaded file

                    DatabaseReference reference = database.getReference();

                    reference.child("Uploads").child(fileName).setValue(url).addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {

                            if (task.isSuccessful())
                                Toast.makeText(Upload.this, "File Uploaded Successfully", Toast.LENGTH_SHORT).show();
                            else
                                Toast.makeText(Upload.this, "Upload failed", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {

            Toast.makeText(Upload.this, "Upload failed",Toast.LENGTH_SHORT).show();
        }
Zoe
  • 27,060
  • 21
  • 118
  • 148
Andrew Le
  • 31
  • 2

1 Answers1

2

storageRef.child("Uploads").child(fileName).getDownloadUrl(); returns a Task object, not Uri directly. You have to add completion listener to this task and then upload the url to your database.

final String fileName = songUri.getLastPathSegment() + "";
//final String fileName1=songUri.getLastPathSegment()+"";

final StorageReference storageRef = storage.getReference();


storageRef.child("Uploads").child(fileName).putFile(songUri)
 .addOnSuccessListener(new OnSuccessListener < UploadTask.TaskSnapshot > () {
   @Override
   public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

     storageRef.child("Uploads").child(fileName).getDownloadUrl()
      .addOnCompleteListener(new OnCompleteListener < Uri > () {
        @Override
        public void onComplete(@NonNull Task < Uri > task) {
         if (task.isSuccessful()) {
          Uri downloadUri = task.getResult();
          reference.child("Uploads").child(fileName).setValue(downloadUri.toString()).addOnCompleteListener(new OnCompleteListener < Void > () {
           @Override
           public void onComplete(@NonNull Task < Void > task) {

            if (task.isSuccessful())
             Toast.makeText(Upload.this, "File Uploaded Successfully", Toast.LENGTH_SHORT).show();
            else
             Toast.makeText(Upload.this, "Upload failed", Toast.LENGTH_SHORT).show();
           }
          });
         } else {
          Toast.makeText(Upload.this, "upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
         }
        }
       }
      }).addOnFailureListener(new OnFailureListener() {
      @Override
      public void onFailure(@NonNull Exception e) {

       Toast.makeText(Upload.this, "Upload failed", Toast.LENGTH_SHORT).show();
      }
Umar Hussain
  • 3,461
  • 1
  • 16
  • 38