void uploadCompImage(){
if (compliantImageUri != null) {
StorageReference fileReference = mStorageRef.child(type).child(victimNameid.getText().toString()+"."+getFileExtension(compliantImageUri));
fileReference.putFile(compliantImageUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(NewCompliant.this,"UPLOAD SUCCESSFUL",Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(NewCompliant.this,"IMAGE UPLOAD FAILED",Toast.LENGTH_SHORT).show();
}
});
fileReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String imgUrl;
imgUrl = uri.toString();
uploadCompData(imgUrl);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
imgSearch();
}
});
} else {
Toast.makeText(NewCompliant.this,"IMAGE IS NOT SELECTED",Toast.LENGTH_SHORT).show();
}
}
private void imgSearch(){
StorageReference imgRef = mStorageRef.child(type).child(victimNameid.getText().toString()+"."+getFileExtension(compliantImageUri));
imgRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
imageUrl = uri.toString();
uploadCompData(imageUrl);
}
});
}
Asked
Active
Viewed 52 times
-1

Peter Haddad
- 78,874
- 25
- 140
- 134
-
Does this answer your question? [Firebase android count children and store it in a variable](https://stackoverflow.com/questions/41163512/firebase-android-count-children-and-store-it-in-a-variable) – Ryan M Aug 05 '21 at 09:58
1 Answers
0
fileReference.putFile(compliantImageUri)
is asynchronous the image will take some time to upload to that storage reference but fileReference.getDownloadUrl()
will execute immediately after that line so you are immediately trying to access the download url of the image that is not uploaded yet that is why you are getting the the error , the image is uploaded when the
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(NewCompliant.this,"UPLOAD SUCCESSFUL",Toast.LENGTH_SHORT).show();
}
is executed , so access the download url in that method or anytime after this method is returned.
HAVE A GOOD DAY

Abhinav Chauhan
- 1,304
- 1
- 7
- 24