如何在Android Studio中获取Firebase存储映像URL?

时间:2018-09-03 13:46:31

标签: android firebase firebase-storage

我想在Firebase Storage的imageView上显示图像而不将图片保存在设备中,所以我尝试使用突出显示的URL(来自下面的照片),它可以工作,但是我不想写地址手动。 如何使用代码获取地址? ps我尝试了dataBaseStorageRef.getDownloadUrl(),它给了我另一个URL,而不是图片中的URL。

enter image description here

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    final StorageReference filePath = storage.child("books").child(""+AddBook.book_id+1).child("photo.jpg");
    if(requestCode == RESULT_GALLERY){
        if (resultCode == RESULT_OK){
            pic_uri = data.getData();
            book_img.setImageURI(pic_uri);
            filePath.putFile(pic_uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Toast.makeText(AddBookActivity.this, "Upload done", Toast.LENGTH_SHORT).show();
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(AddBookActivity.this, "Failed to upload!", Toast.LENGTH_SHORT).show();

                }
            });
            img_addr = filePath.getDownloadUrl().toString();


            Toast.makeText(this, "picture was selected", Toast.LENGTH_SHORT).show();
        }
    }
}

然后,我正在尝试显示其他活动中img_addr中的图片 并且它不起作用,因为image_addr的地址不好(如果我手动将img_addr替换为附件链接中的地址,则它有效)

    if(books_list.get(i).img_addr!=null){
        Toast.makeText(v.getContext(),books_list.get(i).img_addr,Toast.LENGTH_LONG).show();

        Picasso.with(v.getContext()).load(books_list.get(i).img_addr).resize(50,50).into(img);

    }

感谢那些提供帮助的人

3 个答案:

答案 0 :(得分:0)

通过向其添加onsuccess和onfailure侦听器,使getDownloadUrl()异步。

您完整的图片上传任务应如下图所示

 photoReference.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                photoReference.getDownloadUrl().addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                       //do something
                    }
                }).addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        //get your image uri here
                        Uri imgUrl = uri;
                        String imgStringUrl = imgUrl.toString();
                    }
                });

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
               // do something
            }
        });

答案 1 :(得分:0)

您可以使用continueWithTask方法返回下载网址

类似以下内容:

final UploadTask uploadTask = filepath.putFile(uri);   
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                                    @Override
                                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                                        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 filepath.getDownloadUrl();

                                            }
                                        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                                            @Override
                                            public void onComplete(@NonNull Task<Uri> task) {
                                                if (task.isSuccessful()) {
                                                    thumb_download_url = task.getResult().toString();


                                                }
                                            }
                                        });

                                    }
                                }).addOnFailureListener(new OnFailureListener() {
                                    @Override
                                    public void onFailure(@NonNull Exception e) {
                                    }
                                });

答案 2 :(得分:0)

您可以通过以下方法获取下载网址:

private void uploadToStorage(String imgPath, String fileName) {

    Uri file = Uri.fromFile(new File(imgPath));
    StorageReference storageReference = mStorageRef.child(userId + "/images/" + fileName);

    storageReference.putFile(file).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            Task<Uri> downloadUrl = taskSnapshot.getStorage().getDownloadUrl();
            downloadUrl.addOnCompleteListener(new OnCompleteListener<Uri>() {
                @Override
                public void onComplete(@NonNull Task<Uri> task) {
                    Log.v(TAG, "Media is uploaded");

                    String downloadURL = "https://" + task.getResult().getEncodedAuthority() 
                            + task.getResult().getEncodedPath() 
                            + "?alt=media&token=" 
                            + task.getResult().getQueryParameters("token").get(0);

                    Log.v(TAG, "downloadURL: " + downloadURL);
                    //save your downloadURL
                }
            });
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            Log.v(TAG, "Media is not uploaded");
            Log.v(TAG, "Exception: " + exception.getLocalizedMessage());
        }
    });
}

您还可以从task.getResult()下的方法获取方案名称和查询