getDownloadUrl Firebase 存储 returns 空 (Android)

getDownloadUrl Firebase Storage returns Null (Android)

我正在开发一个小项目,我有一些关于 firebase 存储和 getDownload 的问题Url。 我已经在 FirebaseStorage 上上传了一些图片,但是当我尝试下载时 Url 它 return nulls.

代码如下: 进口:

import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;

函数 getImage()

public void getImage(){
    StorageReference myStorage = FirebaseStorage.getInstance().getReference();
    StorageReference newStorage = myStorage.child("picture").child("pic_one.jpg");
    newStorage.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            myuri = uri;
        }
    });
}

没有任何身份验证的 Firebase 存储规则

service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
  allow read, write;   
    }
  }
}

当应用程序运行时,getDownloadUrl 行没有做任何事情,我的意思是我想检索 https link 以使用滑行在另一个 activity 中显示图片,但我只是在 myuri 变量上得到 null 。 变量 myuri 定义为 URI。

提前致谢。

尝试这样做:

private String generatedFilePath;

 myStorage.child("picture").child("pic_one.jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            // Got the download URL for 'pic_one.jpg'
            Uri downloadUri = taskSnapshot.getMetadata().getDownloadUrl();
            generatedFilePath = downloadUri.toString(); /// The string(file link) that you need
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle any errors
        }
    });

此外,无法从存储树的根部获取下载URL。您应该以编程方式将文件的下载 URL 存储到您的数据库,以便以后访问它,因此首先将照片上传到您的存储,然后在 onSuccess 中,您应该将下载 URL 上传到您的数据库,然后从那里检索它。

为此,您应该首先声明您的数据库引用

private DatabaseReference mDatabase;
// ...
mDatabase = FirebaseDatabase.getInstance().getReference();

然后,在您成功将图片上传到存储后,获取下载 URL 并 post 将其添加到您的数据库

这是官方文档中的例子

    Uri file = Uri.fromFile(new File("path/to/images/rivers.jpg"));
    StorageReference riversRef = storageRef.child("images/"+file.getLastPathSegment());
    uploadTask = riversRef.putFile(file);
    
    // Register observers to listen for when the download is done or if it fails
    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle unsuccessful uploads
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
            Uri downloadUrl = taskSnapshot.getDownloadUrl(); //After uploading your picture you can get the download url of it

mDatabase.child("images").setValue(downloadUrl); //and then you save it in your database
            
        }
    });

然后记得像这样从数据库中获取下载 URL:

mDatabase.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
    String downloadURL = dataSnapshot.getValue(String.class);
    //do whatever you want with the download url
  }