如何解决java.lang.IllegalStateException:任务尚未完成将映像上传到Firebase存储时出错?

时间:2018-08-04 06:22:19

标签: android firebase firebase-storage

将Firebase存储更新到最新版本16.0.1后,我开始出现此错误。我没有更改代码中的任何内容,而是在升级gradle build依赖项后才得到此错误

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

        }
    });

2 个答案:

答案 0 :(得分:2)

我想通了,不知道上传任务api有所改变,它使用一个延续来检索下载uri,如下所示:

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 adsImagesRef.getDownloadUrl();
        }
    }).addOnCompleteListener(new OnCompleteListener<Uri>() {
        @Override
        public void onComplete(@NonNull Task<Uri> task) {
            if (task.isSuccessful()) {
                Uri downloadUri = task.getResult();
                if (downloadUri == null)
                    return ;
}
    });

答案 1 :(得分:0)

使用以下代码,不会出现任何错误:

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

我希望这会解决您的错误:)