无法从Firebase获取下载URL

时间:2018-06-20 12:09:56

标签: android firebase firebase-storage

我正在尝试从Firebase获取下载URL,但它为我提供了另一个链接,例如“ com.google.android.gms.tasks.zzu@3689a168”,而不是它在Firebase升级之前为我提供的较早链接。在升级Firebase之前不存在此问题,我正在尝试获取类似于以下“ https://firebasestorage.googleapis.com/v0/b/lbsny-1a3d9.appspot.com/o/pOa47ktSz7ZRFVeykiz63bsm1Hu2%2F1523658820692.jpg?alt=media&token=c2fe1008-bb4a-47f2-9da7-82a3298a55da”的链接

我上传图片的代码

mStorageRef = FirebaseStorage.getInstance().getReference().child(current_uid).child(System.currentTimeMillis() + ".jpg" );
        mStorageRef.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                Task<Uri> downloadUri=mStorageRef.getDownloadUrl();
                String imageUrl = mStorageRef.getDownloadUrl().toString();
                mDataRef = FirebaseDatabase.getInstance().getReference();

                Map map = new HashMap();
                map.put("imageUrl",imageUrl);
                map.put("price",TempImageName);
                map.put("caption",TempImageCaption);
                map.put("time", ServerValue.TIMESTAMP);

                ItemUploadInfo itemUploadInfo = new ItemUploadInfo(TempImageCaption,TempImageName,mStorageRef.getDownloadUrl().toString());

                String ImageUploadId = mDataRef.push().getKey();
                mDataRef.child("Items").child(current_uid).child(ImageUploadId).setValue(map);
            }
        });

1 个答案:

答案 0 :(得分:1)

根据docs,上传文件后,您可以通过调用getDownloadUrl()上的StorageReference方法来获取下载文件的URL:

 final StorageReference ref = storageRef.child("images/mountains.jpg");
uploadTask = ref.putFile(file);

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 ref.getDownloadUrl();
  }
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
    if (task.isSuccessful()) {
        Uri downloadUri = task.getResult();
    } else {
        // Handle failures
        // ...
    }
   }
 });