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

天大地大妈咪最大 提交于 2020-12-30 08:33:33

问题


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

        }
    });

回答1:


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

       }
        });



回答2:


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



来源:https://stackoverflow.com/questions/51683067/how-to-resolve-java-lang-illegalstateexception-task-is-not-yet-complete-error-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!