如何返回上传图片的网址

时间:2019-02-19 17:47:26

标签: android firebase firebase-storage

尝试使用此docs获取在Firebase Storage中上载的图像的URL,但是没有有关如何从downloadUri方法返回Task.continueWithTask值的信息。

即使method documentation也无助于理解。

从文档引用,没有解释如何获取此URL:

上传文件后,您可以通过调用StorageReference上的getDownloadUrl()方法来获取下载文件的URL:

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
                    // ...
                }
            }
        });

2 个答案:

答案 0 :(得分:1)

我已经有一段时间没有使用Firebase了,但是我过去的使用方式是在调用library(shiny) selectInputChoices = 1:10 ui <- fluidPage(mainPanel( actionButton("previous_q", "Previous"), actionButton("next_q", "Next"), selectInput( "jump", "Jump to Question", choices = selectInputChoices, selected = 1 ), textOutput("selected") )) server <- function(input, output, session) { # Select based on "Previous" and "Next" buttons ------- observeEvent(input$previous_q, { req(input$jump) if (as.integer(input$jump) > min(selectInputChoices)) { newSelection <- as.integer(input$jump) - 1 updateSelectInput(session, inputId = "jump", selected = newSelection) } }) observeEvent(input$next_q, { req(input$jump) if (as.integer(input$jump) < max(selectInputChoices)) { newSelection <- as.integer(input$jump) + 1 updateSelectInput(session, inputId = "jump", selected = newSelection) } }) # Display selected output$selected <- renderText({ req(input$jump) paste(input$jump) }) } shinyApp(ui = ui, server = server) 之后分配SuccessListenerOnCompleteListener

  

示例

ref.getDownloadUrl()

答案 1 :(得分:0)

要检索图像或存储在Firestore中的数据的URL,应使用OnSuccessListener

     firepath.putFile(resultUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {     
@Override

public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

    firepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {

@Override
public void onSuccess(Uri uri) {
final String url = uri.toString();
 }});});

此处的字符串URL将具有图像或视频的链接