从Firebase上传字符串方法返回图像URL

时间:2019-05-21 09:30:53

标签: java android firebase

您好,我试图在图像上传后从Firebase存储中检索图像URL,并通过Image Upload方法将其显示在Toast中,该方法会将URL作为字符串返回。

我能够获取URL,但是我的吐司在图像上传完成之前运行,因此返回了空字符串响应来代替URL。

我想要实现的是获取图像URL,如果上传成功,则将其显示在Upload方法之外的烤面包中

这是我尝试过的;

      insertImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(TestFirebaseImageUpload.this,  firebaseImageUpload(filePath), Toast.LENGTH_SHORT).show();
            }
        });

单击insertImage按钮时,它将显示一个吐司,该吐司应包含通过将文件uri发送到firebaseImageUploadMethod获得的图像的URL。

这是我的firebaseImage上传方法

    public String firebaseImageUpload(Uri filePath) {
        if (filePath != null) {
            // Setting progressDialog Title.
            progressDialog = new ProgressDialog(this);
            progressDialog.setTitle(" Uploading...");
            progressDialog.show();

            ref = imagePath.child("images/" + UUID.randomUUID().toString());
            ref.putFile(filePath)
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            progressDialog.dismiss();
                            Toast.makeText(TestFirebaseImageUpload.this, "Uploaded ... ", Toast.LENGTH_SHORT).show();
                            ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                                @Override
                                public void onSuccess(Uri uri) {
                                    URL = uri.toString();
                                }
                            });
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            progressDialog.dismiss();
                            URL = "";
                            Toast.makeText(TestFirebaseImageUpload.this, "Not Uploaded ... ", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                            double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
                            progressDialog.setMessage("Uploaded " + (int) progress + "%");
                        }
                    });
        }
        return URL;
    }

我想要的是在单击“ insertImage”按钮时显示的烤面包中显示URL。

谢谢

1 个答案:

答案 0 :(得分:2)

您可以设置回调

onClick(){
      firebaseImageUpload(filePath, Callback)
    }

    onUploaded(){
      // Display toast here
    }

ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
    URL = uri.toString(); 
    callback.onUploaded(URL);
    }
    });

     interface Callback{
       void onUploaded(String url)
     }