如何在最新版本中使用getdownloadurl?

sai*_*tty 0 android firebase firebase-realtime-database firebase-storage

                Uri downloaduri=taskSnapshot.getDownloadUrl();//here i cant use getdownloadurl() function
                DatabaseReference new_prod=db.push();
                new_prod.child("product name").setValue(prod_name);
                new_prod.child("product price").setValue(prod_price);
                new_prod.child("available stock").setValue(prod_quan);
                new_prod.child("product image").setValue(downloaduri);
                pd.dismiss();//fragments code
Run Code Online (Sandbox Code Playgroud)

我无法使用getdownloadurl。我已经将图像存储在Firebase存储器中了吗?这是限制使用getdownloadurl的片段吗?我的动机是要进行查询以存储在firebase中。请帮助我。

Fra*_*len 5

taskSnapshot.getDownloadUrl()方法已在Firebase Storage SDK的最新版本中删除。您现在需要从StorageReference现在获取下载URL 。

调用StorageReference.getDownloadUrl()返回一个Task,因为它需要从服务器检索下载URL。因此,您需要一个完成侦听器来获取实际的URL。

有关下载文件文档中

 storageRef.child("users/me/profile.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
        // Got the download URL for 'users/me/profile.png' in uri
        System.out.println(uri.toString());
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle any errors
    }
});
Run Code Online (Sandbox Code Playgroud)

或者,在您的情况下,第一行可能是这样:

taskSnapshot.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
Run Code Online (Sandbox Code Playgroud)

另请参阅: