开发者社区> 问答> 正文

如何在Firebase实时数据库中保存图像URL?

我想将图片网址保存在实时数据库中,但我尝试过的解决方案都不能解决问题,我编写的代码将图片保存在Firebase存储中,但我想将图片网址保存在实时数据库中

此代码只是将图像保存在firebase存储中

private Button btn_upload,btn_choose; private ImageView imageView; private Uri filepath; private FirebaseStorage storage; private StorageReference storageReference;

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

btn_choose= (Button) findViewById(R.id.btn_choose);
btn_upload= (Button) findViewById(R.id.btn_upload);
imageView= (ImageView) findViewById(R.id.myImage);

storage = FirebaseStorage.getInstance();
storageReference = storage.getReference();

btn_upload.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        uploadImage();

    }
});

btn_choose.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        chooseImage();

    }
});

}

// choose image function code

private void uploadImage() {

if(filepath!=null)     {
    final ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setTitle("Uploading...");
    progressDialog.show();

    StorageReference reference = storageReference.child("images/" + UUID.randomUUID().toString());

    reference.putFile(filepath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            progressDialog.dismiss();

            Toast.makeText(MainActivity.this, "Image uploaded", Toast.LENGTH_SHORT) .show();
        }
    })
            .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {

                    double progres = (100.0*taskSnapshot.getBytesTransferred()/taskSnapshot.getTotalByteCount());
                    progressDialog.setMessage("Uploaded"+(int)progres+"%");
                }
            }) ;


}   }

我想将图像存储在实时数据库中

展开
收起
小六码奴 2019-10-09 18:56:45 1087 0
1 条回答
写回答
取消 提交回答
  • 上传完成后,您可以请求文件的下载URL。然后将URL添加到实时数据库

    reference.putFile(filepath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { @Override public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { progressDialog.dismiss();

                Toast.makeText(MainActivity.this, "Image uploaded", Toast.LENGTH_SHORT) .show();
                reference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                      //You will get donwload URL in uri
                      Log.d(TAG, "Download URL = "+ uri.toString());
                      mDatabase.child("image").setValue(uri.toString());
                    }
                });
            }
        })
    
    2019-10-09 18:57:09
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
DTCC 2022大会集锦《云原生一站式数据库技术与实践》 立即下载
阿里云瑶池数据库精要2022版 立即下载
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载