如何从Firebase存储中检索图像网址以进行JSON解析?

时间:2018-10-02 12:43:13

标签: java android json firebase firebase-storage

我当时正在考虑在Android中制作View-pager。从服务器检索滑块图像,以便我可以随时更改这些滑块图像。我知道为此,我必须以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?

1 个答案:

答案 0 :(得分:1)

是的,您已正确存储它。可以使用以下代码获取下载网址:

只需用图像路径替换路径,例如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

要查看完整的示例,可以使用此链接: https://github.com/firebase/snippets-android/blob/1c8f86671919fccd43178488fc4799d49f27d786/storage/app/src/main/java/com/google/firebase/referencecode/storage/StorageActivity.java#L309-L332