【发布时间】:2021-09-15 09:19:53
【问题描述】:
我正在尝试通过获取我想要在 putImage 调用的 onSuccessListener 中检索的图像的下载 url 从 Android 应用程序的 Firebase 存储中检索图像,我首先用于上传图像。我将下载 URL 保存在字段变量中,但问题是字段变量未更新。在我尝试将其设置为等于我要检索的图像的下载 URl 之后,我使用 toast 打印出 url 字段变量的值,但它保持不变。我不确定为什么会出现这种情况,所以如果能帮助我解决这个问题,我将不胜感激。
//field variable that s meant to hold download url for the image I want to retrieve
String url = "never changed"
...
final String imageKey = UUID.randomUUID().toString();
StorageReference profileref = storageReference.child("contactProfiles/" + imageKey);
profileref.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Task<Uri> task = taskSnapshot.getMetadata().getReference().getDownloadUrl();
task.addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
//setting url equal to url of the image I want to retrieve
url = uri.toString();
}
});
Toast.makeText(ContactCreation.this, "Successful Profile Picture Upload", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(ContactCreation.this, "Failed to Upload Profile Picture", Toast.LENGTH_SHORT).show();
}
});
//Seeing if the url changed or not
Toast.makeText(ContactCreation.this, url, Toast.LENGTH_SHORT).show();
【问题讨论】:
-
请仅使用
android-studio标签来回答有关 Android Studio IDE 本身的问题。对于一般的 Android 编程问题,请使用android标签。
标签: android firebase firebase-realtime-database firebase-storage