首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >替换过时的taskSnapshot.getDownloadUrl().toString();

替换过时的taskSnapshot.getDownloadUrl().toString();
EN

Stack Overflow用户
提问于 2019-08-08 12:55:56
回答 3查看 229关注 0票数 0

我正在学习一个android教程,并且我一直坚持使用被弃用的方法。我无法获取下载url以使我能够在ImageView中显示图像

代码语言:javascript
运行
复制
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICTURE_RESULT && resultCode == RESULT_OK) {
            Uri imageUri = data.getData();
            StorageReference ref = FirebaseUtil.mStorageRef.child(imageUri.getLastPathSegment());
            ref.putFile(imageUri).addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    String url = taskSnapshot.getDownloadUrl().toString();
                    String pictureName = taskSnapshot.getStorage().getPath();
                    deal.setImageUrl(url);

如何修改"String url = taskSnapshot.getDownloadUrl().toString();"行才能获得下载链接?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-08-08 13:07:40

getDownloadUrl()已弃用,因此您需要使用任务并检查isComplete(),如下所示以查看是否已完成

代码语言:javascript
运行
复制
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICTURE_RESULT && resultCode == RESULT_OK) {
        Uri imageUri = data.getData();
        StorageReference ref = FirebaseUtil.mStorageRef.child(imageUri.getLastPathSegment());
        ref.putFile(imageUri).addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                    Task<Uri> uri = taskSnapshot.getStorage().getDownloadUrl();
                    while(!uri.isComplete());
                    Uri url = uri.getResult();

                    Log.i(TAG, url.toString());
                    deal.setImageUrl(url.toString());
票数 1
EN

Stack Overflow用户

发布于 2019-08-08 13:13:32

尝尝这个

代码语言:javascript
运行
复制
taskSnapshot.getMetadata().getReference().getDownloadUrl().toString()
票数 1
EN

Stack Overflow用户

发布于 2019-08-08 13:44:01

尝尝这个

代码语言:javascript
运行
复制
StorageReference imageRef = FirebaseUtil.mStorageRef.child(imageUri.getLastPathSegment());
    UploadTask uploadTask = imageRef.putFile(imageUri);

    uploadTask.continueWithTask(task -> {
        if (!task.isSuccessful()) {
            if (task.getException() != null)
                throw task.getException();
        }
        return imageRef.getDownloadUrl();
    }).addOnCompleteListener(task -> {
        if (task.isSuccessful()) {
               Uri downloadUri = task.getResult()

        } else {
            if (task.getException() != null)
                task.getException().printStackTrace();
        }
    });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57405414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档