如何在最新版本中使用 getdownloadurl? [英] How to use getdownloadurl in recent versions?

查看:37
本文介绍了如何在最新版本中使用 getdownloadurl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 Uri downloaduri=taskSnapshot.getDownloadUrl();//这里不能使用getdownloadurl()函数数据库参考 new_prod=db.push();new_prod.child("产品名称").setValue(prod_name);new_prod.child("产品价格").setValue(prod_price);new_prod.child("可用库存").setValue(prod_quan);new_prod.child("产品图片").setValue(downloaduri);pd.dismiss();//片段代码

我无法使用 getdownloadurl .我已经在 firebase 存储中存储了图像.是否是限制使用 getdownloadurl 的片段?我的动机是查询以存储在 firebase 中.请帮助我.

解决方案

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

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

来自有关下载文件的文档:

<块引用>

 storageRef.child("users/me/profile.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener() {@覆盖公共无效onSuccess(Uri uri){//得到 uri 中 'users/me/profile.png' 的下载 URLSystem.out.println(uri.toString());}}).addOnFailureListener(new OnFailureListener() {@覆盖public void onFailure(@NonNull 异常异常){//处理任何错误}});

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

taskSnapshot.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener() {

另见:

                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

I was unable to use getdownloadurl .I have already stored image in the firebase storage.Is it the fragment that is restricting the use of getdownloadurl? My motive is to make a query to store in firebase.please help me.

解决方案

The taskSnapshot.getDownloadUrl() method was removed in recent versions of the Firebase Storage SDK. You will need to instead get the download URL from the StorageReference now.

Calling StorageReference.getDownloadUrl() returns a Task, since it needs to retrieve the download URL from the server. So you'll need a completion listener to get the actual URL.

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

Alternatively that first line could be this if you're getting the download URL right after uploading (as in your case):

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

Also see:

这篇关于如何在最新版本中使用 getdownloadurl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆