Odai A. Ali
Odai A. Ali

Reputation: 1235

how to resolve java.lang.IllegalStateException: Task is not yet complete Error when uploading image to Firebase storage?

I started to have this error after I updated firebase storage to the latest version 16.0.1. I didn't change anything in my code i just got this error after upgrading gradle build dependencies

AskFirebase

uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {


            if (task.isSuccessful()) {
                Uri downloadUri = task.getResult().getStorage().getDownloadUrl().getResult();

                if (downloadUri == null)
                    return ;


                downloadUriArray.add(String.valueOf(downloadUri));

                singleAdImageArrayList.get(uriIndex).setUploading(false);
                singleAdImageArrayList.get(uriIndex).setImgDownloadUri(downloadUri);
                singleAdImageArrayList.get(uriIndex).setSent(true);
                singleAdImageArrayList.get(uriIndex).setHasLocalUri(false);
                sendImagesUpdateToActivity();

                checkCompletion();

                Toast.makeText(getApplicationContext(), "sent successfully", Toast.LENGTH_LONG).show();
                return ;

            }else {

                singleAdImageArrayList.get(uriIndex).setUploading(false);
                singleAdImageArrayList.get(uriIndex).setFailUploading(true);
                sendImagesUpdateToActivity();

                failuresCounter++;
                if (task.getException() != null && task.getException().getMessage() != null) {
                    Log.d(TAG, "onComplete: failed exception: " + task.getException().getMessage());
                    QuickToastUtil.makeToast(getApplicationContext(), getString(R.string.network_error), false);
                }

                checkCompletion();

                return ;
            }

        }
    });

Upvotes: 8

Views: 12319

Answers (2)

Odai A. Ali
Odai A. Ali

Reputation: 1235

I figured it out ,didn't know that uploading task api has changed a little bit, it uses a continuation to retrieve the download uri just as follow:

final StorageReference imagesRef= storageRef.child("images/my_image.jpg");
urlTask = imagesRef.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 imagesRef.getDownloadUrl();
            }
        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult();
                    if (downloadUri == null)
                        return ;
                    else
                    doWhateverYouWant(downloadUri);

       }
        });

Upvotes: 12

Aman kumar Rahi
Aman kumar Rahi

Reputation: 1

Use the following code and you won't get any error:

String downloadUri= task.getMetadata().getReference().getDownloadUrl().toString();

I hope, this will fix your error :)

Upvotes: -4

Related Questions