How to upload multiple images on firebase storage [duplicate]

你。 提交于 2021-02-05 10:45:10

问题


I uploaded four images on firebase storage now I want to store the four images reference in a single json tree in the firebase database. The code i have adds the images url reference for a single image to the four different fields in the database, instead of inserting each image Url reference to its respective field in the database. How can i get the specific url reference for each image and insert it in the field meant for it in the firebase database. Below is my uploadImage method.

enter code here

private  void uploadImage(final String note){
    int j=0;
    Uri[] uri=new Uri[selectData.size()];
    final ArrayList<String> storeTask = new ArrayList<>();

while(j<selectData.size()){ uri[j] = selectData.get(j);

        storage = FirebaseStorage.getInstance();
        storageReference = storage.getReference();
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("Uploading.....");
        progressDialog.show();
        ref = storageReference.child("/" + note
                + "/" + uri[j].getLastPathSegment());

        //final int i=j;
      uploadTask = ref.putFile(uri[j]);
final int finalJ = j;
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                progressDialog.dismiss();

                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();
                        }
                        return ref.getDownloadUrl();
                    }
                }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                    @Override
                    public void onComplete(@NonNull Task<Uri> task) {
                        if (task.isSuccessful()) {
                            switch(finalJ){
                                case 0:
                            FirebaseDatabase.getInstance().getReference()
                                    .child("notes")
                                    .child(note)
                                    .child("attach_url").setValue(task.getResult().toString());
                            break;
                                case 1:
                                    FirebaseDatabase.getInstance().getReference()
                                            .child("notes")
                                            .child(note)
                                            .child("attach_url1").setValue(task.getResult().toString());
                                    break;
                                case 2:
                                    FirebaseDatabase.getInstance().getReference()
                                            .child("notes")
                                            .child(note)
                                            .child("attach_url2").setValue(task.getResult().toString());
                                    break;
                                case 3:
                                    FirebaseDatabase.getInstance().getReference()
                                            .child("notes")
                                            .child(note)
                                            .child("attach_url3").setValue(task.getResult().toString());
                                    break;
                                default:
                                    break;
                            }

                        }
                    }
                });
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                progressDialog.dismiss();
                Toast.makeText(NewNoteActivity.this, "Failed" + e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onProgress(@NonNull UploadTask.TaskSnapshot snapshot) {
                double progress = (100.0 * snapshot.getBytesTransferred() / snapshot.getTotalByteCount());
                progressDialog.setMessage("Uploaded "  +  (int) progress + "%");
            }
        });
j++;
    }

}

来源:https://stackoverflow.com/questions/64777243/how-to-upload-multiple-images-on-firebase-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!