我正在考虑在android中制作View-pager。它从服务器检索 slider 图像,以便我可以随时更改这些 slider 图像。我知道为此我必须传递 JSON 格式的图像 URL。但我不明白如何获取这些图像 url,以便我可以以 JSON 格式传递这些图像 url。
我尝试将图像存储在 Firebase 的存储中。我不知道这样的方法对不对? I uploaded this image on firebase storage below this image there is a storage location url can i use that storage location url in json?
最佳答案
是的,您已正确存储。您可以使用以下代码获取下载网址:
只需将路径替换为您的图像路径,例如images/6401.jpg
final StorageReference ref = storageRef.child("path");
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()) {
//Your image url do something with it.
Uri downloadUri = task.getResult();
} else {
// Handle failures
// ...
}
}
});
有关这方面的更多信息,请访问此网站: https://firebase.google.com/docs/storage/android/upload-files
或者
关于java - 如何从 Firebase 存储中检索图像 url 以进行 JSON 解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52608596/