0

I am trying to upload image captured from camera into the firebase storage in the form of byte[]. I am able to successfully upload the image but I am unable to get its download url. It gives me an error saying

java.lang.IllegalStateException: Task is not yet complete

Here is my code

String filePathAndName = "product_images/"+""+timestamp;
            StorageReference storageReference = FirebaseStorage.getInstance().getReference(filePathAndName);

            Bitmap bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), image_uri);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.JPEG, 25, baos);
            byte[] data = baos.toByteArray();
            UploadTask uploadTask2 = storageReference.putBytes(data);
            uploadTask2.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {


                }
            });
            uploadTask2.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Toast.makeText(AddProductActivity.this, "Upload successful", Toast.LENGTH_LONG).show();



                    Task<Uri> uriTask = taskSnapshot.getStorage().getDownloadUrl();
                    Uri downloadImageUri = uriTask.getResult();
                    while (!uriTask.isSuccessful());
                    if (uriTask.isSuccessful()){
                        /*url of image received, upload to db*/
                        /*setup data to upload*/
                        progressDialog.dismiss();
                        Toast.makeText(AddProductActivity.this, "uriTask Successful", Toast.LENGTH_SHORT).show();
                    } else {
                        progressDialog.dismiss();
                        Toast.makeText(AddProductActivity.this, "uriTask Unsuccessful", Toast.LENGTH_SHORT).show();

                    }




                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(AddProductActivity.this, "Upload Failed -> " + e, Toast.LENGTH_LONG).show();
                }
            });

I am successfully uploading the image and getting the toast message of success but the error arises at the following line

Task uriTask = taskSnapshot.getStorage().getDownloadUrl();

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441

0 Answers0