我正在尝试访问Firebase上的已上传图像的实际URL,但没有获得。
看看下面的代码:
String ActualImageUrl = null;
taskSnapshot.getMetadata().getReference().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>()
{
@Override
public void onSuccess(Uri uri)
{
ActualImageUrl = uri.toString();
Log.d("okok","url inside: "+ActualImageUrl);
}
});
Log.d("okok","url ouside: "+ActualImageUrl);
好吧:里面的网址:实际网址 好的:URL以外:空
答案 0 :(得分:1)
在Firebase Official guide中使用此代码获取下载网址
final StorageReference ref = storageRef.child("images/mountains.jpg");
uploadTask = ref.putFile(file);
Task<Uri> urlTask = uploadTask.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();
}
// Continue with the task to get the download URL
return ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
} else {
// Handle failures
// ...
}
}
});