我正在将图像上传到 Firebase Storage,我需要存储中的图像 URL 才能使用它,然后将其插入 FireStore 中的文档中。 如何在上传过程完成后而不是完成之前获取 url?
public String uploadImage(byte[] bytes) {
try {
final StorageReference ref = storage.child("images/" + new Date().toString());
ref.putBytes(bytes)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
res = uri.toString();
return;
}
});
}
});
System.out.println("RES : " + res);
return res;
}catch (NullPointerException e){
return null;
}
}
//the variable res must return a not null value.
最佳答案
引用::Image not retrieving from Firebase
//add file on Firebase and got Download Link
filePath.putFile(imageUri).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();
}
return filePath.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()){
Uri downUri = task.getResult();
Log.d(TAG, "onComplete: Url: "+ downUri.toString());
}
}
});
关于java - 使上传方法返回字符串 "the uploaded file url",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55665006/