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
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 ;
}
}
});
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.
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.
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.
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);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With