如何在Firebase中连接数据库条目和存储条目?

时间:2017-12-12 10:40:16

标签: java android

我在使用Firebase的Android应用中有两个功能正常工作。我可以将图像上传到我的存储空间,我可以向我的数据库添加属性,但是如何在两个条目之间建立关系。

这是我的数据库代码:

private void uploadImage() {

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

        StorageReference ref = storageReference.child("images/" + UUID.randomUUID().toString());
        ref.putFile(filePath)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        progressDialog.dismiss();
                        Toast.makeText(uploadActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        progressDialog.dismiss();
                        Toast.makeText(uploadActivity.this, "Failed " + e.getMessage(), 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 + "%");
                    }
                });
    }
}

这是我的存储代码:

.bbb:hover {
 background-color: red;
 font-size: 0;
 opacity: 0;
}
.bbb:hover .fa-address-book { 
  /* not sure how you're hiding your icon initially but add the styles you need here to show your icon when the span is hovered */
  display: block;
}

我希望用户能够选择汽车的图像,输入与该图像相关的属性,如制作/模型,然后按上传按钮将两者都输入到Firebase中,同时了解特定图像与特定属性有关。

1 个答案:

答案 0 :(得分:1)

 StorageReference filess= storageReference.child("images/" + UUID.randomUUID());
    filess.putFile(url)
            .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Log.e("url", taskSnapshot.getDownloadUrl() + ":");

                   // Get Url from here and add to your Database
                    addFirebase(taskSnapshot.getDownloadUrl());
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {

                }
            })
            .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {

                }
            });

Firebase插入数据

     private void addFirebase(String url){
           String make = editTextMake.getText().toString().trim();
           String model = editTextModel.getText().toString().trim();
           String description = getText().toString().trim();
            Car car = new Car(make, model, description,url);

           FirebaseUser user = auth.getCurrentUser();
           databaseReference.child(user.getUid()).setValue(car);

           Toast.makeText(this, "Information Saved....", Toast.LENGTH_LONG).show();

}