如何从“ addOnSuccessListener”内部的firebase存储中获取String中的下载URL

时间:2019-03-17 08:51:35

标签: android firebase-storage

这是我上传图片文件并获取下载链接的方法

UploadTask uploadTask = profileImageRef.putBytes(data);
        uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                String fileUrl = taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();

                Toast.makeText(UserProfile.this, "Uploaded Successfully!", Toast.LENGTH_SHORT).show();
            }});

当我登录fileUrl时,它会显示这样的字符串

com.google.android.gms.tasks.zzu@6ee8ba8

有人请给我一个解决方案,以获取下载网址。

1 个答案:

答案 0 :(得分:0)

您可以使用此

UploadTask uploadTask = profileImageRef.putBytes(data);

    Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
    @Override
    public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
        if (!task.isSuccessful()) {
            throw task.getException();
        }

        // Continue with the task to get the download URL
        return profileImageRef.getDownloadUrl();
    }
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
    @Override
    public void onComplete(@NonNull Task<Uri> task) {
        if (task.isSuccessful()) {
            Uri downloadUri = task.getResult();
        } else {
            // Handle failures
            // ...
        }
    }
});