ArrayList每次实例化(多张图片)

时间:2018-12-06 07:17:22

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

我想要做的是从调用“ upload method”方法的底部的循环中上传URI。我可以看到在Logcat中打印的所有图像URI。

但是在任务的onCompleteListener内部,我每次想到时都会实例化一个新的ArrayList。因此,将图像上传到Cloud Storage很好,但是在数据库中,我仅保存第二个图像URI的路径。!

如何实例化ArrayList,以便可以将所有图像URI保存到数据库中?

   private void uploadMethod(final Uri uri, final int count) {

    String strI = Integer.toString(count);

    final StorageReference storageReference = FirebaseStorage.getInstance().getReference()
            .child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
                    "/post_image".concat(strI));

    UploadTask uploadTask = storageReference.putFile(uri);
    Task<Uri> urlTask = 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 storageReference.getDownloadUrl();
        }
    }).addOnCompleteListener(new OnCompleteListener<Uri>() {
        @Override
        public void onComplete(@NonNull Task<Uri> task) {
            if (task.isSuccessful()) {
                Uri downloadUri = task.getResult();
                ArrayList<Uri> arrayListImageDownloadUri= new ArrayList<>();

                arrayListImageDownloadUri.add(downloadUri);

                Log.d(TAG, "onClick: arraylist "+ arrayListImageDownloadUri);

                if (count == (arrayListImageDownloadUri.size() )) {
                    uploadImageUrlToFirebase(arrayListImageDownloadUri);

                }

            }
        }
    });
}
private void uploadImageUrlToFirebase(ArrayList<Uri>arrayListImageDownloadUri) {

    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

    Map<String, Object> map6 = new HashMap<>();
    ArrayList<String> arrayList = new ArrayList<>();
    for (int i = 0; i < arrayListImageDownloadUri.size(); i++) {
        arrayList.add(arrayListImageDownloadUri.get(i).toString());
    }
    map6.put("imageUrl", arrayList);

    reference.updateChildren(map6).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {

        }
    });

}

@Override
public void onMultiImageSelected(List<Uri> uriList, String tag) {

    for (int i = 0; i < uriList.size(); i++) {
        imageUri = uriList.get(i);
        if (i >= 6) return;
        ImageView iv;
        switch (i) {
            case 0:
                iv = ivImage1;
                break;
            case 1:
                iv = ivImage2;
                break;
            case 2:
                iv = ivImage3;
                break;
            case 3:
                iv = ivImage4;
                break;
            case 4:
                iv = ivImage5;
                break;
            case 5:
            default:
                iv = ivImage6;
        }

        Glide.with(this).load(uriList.get(i)).into(iv);

    }

    for (int j = 0; j < uriList.size(); j++) {
        uploadMethod(uriList.get(j), j);
    }
}

0 个答案:

没有答案