我正在尝试将从摄像机捕获的图像以byte []的形式上传到Firebase存储中。我能够成功上传图像,但无法获取其下载URL。它给我一个错误的说法
java.lang.IllegalStateException:任务尚未完成
这是我的代码
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();
}
});
我已经成功上传了图片并获得了祝酒消息,但错误出现在下一行
任务uriTask = taskSnapshot.getStorage()。getDownloadUrl();