在firebase存储中检索到错误的下载URL [重复]

问题描述 投票:1回答:1

我正在从存储在firebase存储中的歌曲文件中检索下载URL。我获取的URL与firebase存储中的URL不同。

这是我得到的错误链接:com.google.android.gms.tasks.zzu@75f559a

这是正确的链接:https://firebasestorage.googleapis.com/v0/b/fouronesixsound-51999.appspot.com/o/Uploads%2F1221?alt=media&token=56beacd5-9abd-4a74-b294-69eb111fcb00

这是我的数据库设置图片的链接:https://imgur.com/a/Gtl1ThZ

这是我的代码:

  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();
        }
android firebase firebase-realtime-database firebase-storage
1个回答
0
投票

storageRef.child("Uploads").child(fileName).getDownloadUrl();返回一个Task对象,而不是直接返回Uri。您必须向此任务添加完成侦听器,然后将该URL上载到您的数据库。

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();
      }
© www.soinside.com 2019 - 2024. All rights reserved.