Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ;
            }

        }
    });
like image 289
Odai A. Ali Avatar asked Aug 04 '18 06:08

Odai A. Ali


People also ask

What is an IllegalStateException in Java?

Generally, this method is used to indicate a method is called at an illegal or inappropriate time. Example: After starting a thread we are not allowed to restart the same thread once again otherwise we will get Runtime Exception saying IllegalStateException.

Is IllegalStateException checked or unchecked at runtime?

Whether the exception is checked or unchecked every exception occurs at run time only if there is no chance of occurring any exception at compile time. IllegalStateException is the child class of RuntimeException and hence it is an unchecked exception.

Does IllegalStateException need to be declared in the throws clause?

Since the IllegalStateException is an unchecked exception, it does not need to be declared in the throws clause of a method or constructor. The IllegalStateException is thrown when the Java environment or application is not in an appropriate state for the requested operation.


1 Answers

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);

       }
        });
like image 190
Odai A. Ali Avatar answered Oct 22 '22 01:10

Odai A. Ali