android - 最新版本如何使用getdownloadurl?

标签 android firebase 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

我无法使用 getdownloadurl 。我已经将图像存储在 firebase 存储中。是否是该 fragment 限制了 getdownloadurl 的使用?我的动机是进行查询以存储在 firebase 中。请帮助我。

最佳答案

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

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

来自documentation on downloading a file :

 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
    }
});

或者,如果您在上传后立即获得下载 URL(如您的情况),则第一行可能是这样的:

taskSnapshot.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {

另请参阅:

关于android - 最新版本如何使用getdownloadurl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62089148/

相关文章:

firebase - 如何使用 vue 和 vuefire 加载 firebase 数据?

android - 以编程方式原生 Android VPN

android - 文件 :///android_asset/www/index. html 是什么意思?

android - 注销时的 Firebase facebook 按钮 android 身份验证

java - 变量在 onComplete 方法中很好,但在外面它是 null

firebase - Flutter Firebase 存储插件需要花费大量时间来上传文件

ios - 根据 uid 访问 Firebase 存储镜像

android - ClickableSpan 避免触摸传播到父 View

java - QuickContactBadge 使应用程序崩溃 :

node.js - firebase-admin WebSocket 错误