在Firebase存储中检索到错误的下载URL

时间:2019-04-04 04:53:11

标签: android firebase firebase-realtime-database firebase-storage

我正在从存储在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();
        }

1 个答案:

答案 0 :(得分: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();
      }