无法从Firebase获取下载URL

时间:2018-08-11 17:54:39

标签: java android firebase firebase-storage

尝试从Firebase存储中检索图像时出现此错误。

java.lang.IllegalStateException: Task is not yet complete on upload task.

代码:-

Uri imageUri = data.getData();
final StorageReference filePath = UserProfileImageRef.child(currentUserID + ".jpg");

filePath.putFile(imageUri)
    .addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {

        @Override
        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
            if (task.isSuccessful()) {
                Toast.makeText(ProfileActivity.this, "Profile Image Uploaded", Toast.LENGTH_SHORT).show();

                String downloadUri = filePath.getDownloadUrl().getResult().toString();
                UserRef.child("profileImage").setValue(downloadUri).addOnCompleteListener(new OnCompleteListener<Void>() {
                     @Override
                     public void onComplete(@NonNull Task<Void> task) {
                         if (task.isSuccessful()) {

                             Intent selfIntent = new Intent(ProfileActivity.this,ProfileActivity.class);
                             startActivity(selfIntent);
                             Toast.makeText(ProfileActivity.this, "Profile image uploaded to database", Toast.LENGTH_SHORT).show();
                             progressDialog.dismiss();
                         } 
                         else {
                             String message = task.getException().getMessage();
                             Toast.makeText(ProfileActivity.this, "Error Occurred" + message, Toast.LENGTH_SHORT).show();
                             progressDialog.dismiss();
                         }
                      }
                  });
        }
    }
});

1 个答案:

答案 0 :(得分:0)

filePath.getDownloadUrl()返回一个Task,该Task跟踪获取URL所需的异步工作。您不能只是立即致电getResult(),就必须像收听putFile()一样聆听其结果。

阅读此内容以获取更多信息:How to get URL from Firebase Storage getDownloadURL