0

i am trying to upload a file to firestore (with putBytes) and then its url to the database. So i am doing the db update in the "onSuccessListener" of the storage task like this:

private void uploadFile(byte[] bookImg, final String bookName){
    if(mUploadTask != null && mUploadTask.isInProgress()){
        //... upload in progress ...
    } else {
        if (bookImg != null) {

            StorageReference fileRef = mStorageRef.child(bookName);
            mUploadTask = fileRef.putBytes(bookImg)
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                            StorageReference refToUploadedPic = taskSnapshot.getMetadata().getReference();
                            Uri uploadedPicUri = refToUploadedPic.getDownloadUrl().getResult();

                            Upload upload = new Upload(bookName,
                                    uploadedPicUri.toString());

                            String uploadId = mDatabaseRef.push().getKey(); //creates new entry on db
                            mDatabaseRef.child(uploadId).setValue(upload); //saves the file metadata on the new entry
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                          //... handle Failure...
                    })
                    .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                          //... handle onProgress...
                    });
        } else {
            Toast.makeText(this, "NoFile", Toast.LENGTH_LONG).show();
        }
    }
}

the picture is uploaded to the storage but when it gets to this line:

Uri uploadedPicUri = refToUploadedPic.getDownloadUrl().getResult();

i get this error:

java.lang.IllegalStateException: Task is not yet complete at com.google.android.gms.common.internal.Preconditions.checkState(Unknown Source:8) at com.google.android.gms.tasks.zzu.zzdq(Unknown Source:4) at com.google.android.gms.tasks.zzu.getResult(Unknown Source:3) at com.controllers.BookListActivity$4.onSuccess(BookListActivity.java:302) at com.controllers.BookListActivity$4.onSuccess(BookListActivity.java:289) at com.google.firebase.storage.zzi.zza(Unknown Source:13) at com.google.firebase.storage.zzac.zza(Unknown Source:2) at com.google.firebase.storage.zzaf.run(Unknown Source:6) at android.os.Handler.handleCallback(Handler.java:789) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6665) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)

the thing is that when i am debugging and step into this line (with F7) everything works as expected....

any help??

Ofir
  • 159
  • 1
  • 12

1 Answers1

0

think you need to add an OnSuccessListener<Uri> there:

refToUploadedPic.getDownloadUrl()
.addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {

    }
})
.addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {

    }
});
Martin Zeitler
  • 1
  • 19
  • 155
  • 216