1

I am getting this error when trying to retrieve image from firebase storage.

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

Code:-

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();
                         }
                      }
                  });
        }
    }
});
Raj
  • 2,997
  • 2
  • 12
  • 30
Simone Atzeni
  • 21
  • 2
  • 3

1 Answers1

0

filePath.getDownloadUrl() returns a Task that tracks the asynchronous work required to get the url. You can't just call getResult() on it immediately - you have to listen to its results like your are currently with putFile().

Read this for more information: How to get URL from Firebase Storage getDownloadURL

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441