将多个图像或文件上传到firebase中的storge并获取uri,并将uri放入数据库

时间:2018-02-19 02:50:49

标签: java android firebase firebase-realtime-database firebase-storage

我希望有人教我如何将多个文件上传到Firebase存储,并获取这些文件的地址并将其放入Firebase数据库。

2 个答案:

答案 0 :(得分:1)

没有用于上传多个文件的firebase存储方法,但可以通过循环执行文件上传方法来实现: 我希望你有你要上传的所有文件。

尝试以下代码:

// arrayListImageFile是包含文件\

的ArrayList
for (int i = 0; i < arrayListImageFile.size(); i++) {
            Uri uri = Uri.fromFile(arrayListImageFile.get(i));
            uploadMethod(uri, i);
        }

以下是上传方法: // arrayListImageDownloadUri是ArrayLisr \

private void uploadImageMethod(Uri uri, final int i, final String pushKey) {
    final StorageReference imageRef = FirebaseStorage.getInstance().getReference().child(Constants.FIREBASE_FOLDER_INSURANCE +
            "/" + FirebaseAuth.getInstance().getUid() + "/" + pushKey + "/" + i + ".jpg");

    arrayListImageRef.add(imageRef); //arraylist of type StorageRef \\
    imageRef.putFile(uri)
            .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    imageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            arrayListImageDUrl.add(uri);

                            ArrayList<String> imageRefString = new ArrayList<>();
                            for (int i = 0; i < arrayListImageRef.size(); i++) {
                                imageRefString.add(arrayListImageRef.get(i).toString());
                            }

                            ArrayList<String> imageDUrlString = new ArrayList<>();
                            for (int i = 0; i < arrayListImageDUrl.size(); i++) {
                                imageDUrlString.add(arrayListImageDUrl.get(i).toString());
                            }

                            Map<String, Object> map = new HashMap<>();
                            map.put("imageRef", imageRefString);
                            map.put("imageDownloadUrl", imageDUrlString);
                            DatabaseReference dbRefLicenseDoc = FirebaseDatabase.getInstance().getReference(Constants.FIREBASE_NODE_INSURANCE);
                            dbRefLicenseDoc.child(FirebaseAuth.getInstance().getUid()).child(pushKey)
                                    .updateChildren(map).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if (task.isSuccessful()) {
                                        if (i == arrayListImageUri.size() - 1) {
                                            dismissDialog();
                                            finish();
                                        }
                                    }
                                }
                            });

                        }
                    });
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    if (!isFinishing()) {
                        dismissDialog();
                    }
                    Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
                }
            });

}

答案 1 :(得分:0)

要将图像上传到Firebase存储,请使用此方法

private void uploadImage(Uri filePath) {
    //filePath contains the Uri of your image or file
    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();
                        //Here you store the uploaded Image URL as a string
                        imageUrl = taskSnapshot.getDownloadUrl().toString();
                        Toast.makeText(CompleteProfileActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        progressDialog.dismiss();
                        Toast.makeText(CompleteProfileActivity.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+"%");
                    }
                });
    }
}

如果你想上传多个文件,请将其放入循环中,并在每次迭代中传递不同的Uri。

现在您将图片网址作为字符串,您可以将该字符串推送到Firebase数据库。

例如: -

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("ImageUrl").setValue(imageUrl);

在图像视图中显示上传的图像

Picasso.with(getContext()).load(/*read the string url from database*/).into(imageView);

文档始终是开始学习的最佳场所:)

Upload Files To Firebase Storage Docs

Download Files From Firebase Storage Docs

Read and write data from Firebase Database