使用不同的postkeys上传一张图片,但我想将所有图片存储在Firebase数据库中的一个postkey下

时间:2018-06-19 06:39:59

标签: android firebase firebase-realtime-database

One one image uploading with different postkeys but i want to store all images under one postkey

这是使用post key将所有图像上传到firebase存储和数据库的代码,但是一个图像存储有不同的帖子密钥,我想将所有上传的图像存储到firebase数据库中的博客下的一个帖子密钥中。请帮助我_这是我过去几天面临的问题,我想在一个postkey下检索所有上传的图像

 upload.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            UploadTask uploadTask;
            if( selectedImageGridView.getChildCount()!= 0)
            {
                for ( int i = 0; i < selectedImages.size(); i++) {
                    blogimages   =  new ArrayList<>();
                   Uri  uri = Uri.parse("file://"+selectedImages.get(i));
                    final String CurrentUser = firebaseAuth.getCurrentUser().getUid();
                    StorageReference reference = mstorageReference.child("Blog_pics/users").child(uri.getLastPathSegment());
                    uploadTask = reference.putFile(uri);

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

                            final Uri downloaduri = taskSnapshot.getDownloadUrl();
                            Log.v("DOWNLOAD URI", String.valueOf(downloaduri));
                            blogimages.add(downloaduri.toString());
                            Log.v("BLOGGIMAGES", String.valueOf(blogimages));
                            final String key = mdatabaseReference.push().getKey();
                            final String posttitle = desc.getText().toString();
                            final String CurrentUser = firebaseAuth.getCurrentUser().getUid();
                            int l = 0;
                            while (l < blogimages.size()){
                             Log.v("BLOGIMAGESSIZE", String.valueOf(blogimages.size()));
                                Map n = new HashMap();
                                int countingImage = 0;
                                n.put(String.valueOf("img" + countingImage), blogimages.get(l).toString());
                                Log.v("MapCount", String.valueOf(n));
                                mdatabaseReference.child(key).child("images").updateChildren(n).addOnCompleteListener(new OnCompleteListener() {
                                    @Override
                                    public void onComplete(@NonNull Task task) {
                                        if (task.isSuccessful()) {
                                            Toast.makeText(PhotoUploadActivity.this, "got download url", Toast.LENGTH_SHORT).show();

                                        } else{

                                            Toast.makeText(PhotoUploadActivity.this, "Failed to put in db", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });

                                l++;
                                countingImage++;
                            }
                            }

                    }) .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception exception) {
                        }
                    });
                }

            }
        }
    });

1 个答案:

答案 0 :(得分:0)

您必须更改此行

StorageReference reference = mstorageReference.child("Blog_pics/users").child(uri.getLastPathSegment());

此行:您必须将这些行放在 befor for循环

String userId = firebaseAuth.getCurrentUser().getUid();
                    StorageReference reference = mstorageReference.child("Blog_pics/users").child(userId);

然后内部循环

refrence.child(uri.getLastPathSegment());
uploadTask = reference.putFile(uri);
...