1
public class MainActivity extends AppCompatActivity {

    String key;
    private ImageView mImageView;
    private FirebaseStorage mStorageRef;
    private Button mButton;
    private StorageTask mUploadTask;
    private List<Uri> uploadedImages = new ArrayList<>();

    private DatabaseReference mRefre;
    ArrayList<String> filepath = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = (ImageView) findViewById(R.id.hhhh);

        mRefre = FirebaseDatabase.getInstance().getReference();
        mButton = (Button) findViewById(R.id.addphoto);

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                filepath.clear();

                FilePickerBuilder.getInstance().setMaxCount(3)
                        .setSelectedFiles(filepath)
                        .setActivityTheme(R.style.AppTheme)
                        .pickPhoto(MainActivity.this);    
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {

            case FilePickerConst.REQUEST_CODE:

                if (resultCode == RESULT_OK && data != null) {

                    final ArrayList<String> filepath;
                    filepath = data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_PHOTOS);

                    filepath.add("image1");
                    filepath.add("image2");

                    for (String path : filepath) {

                        final Uri file = Uri.fromFile(new File(path));
                        key = mRefre.child("products").push().getKey();

                        StorageReference photoRef = FirebaseStorage.getInstance().getReference("Images").child(uploadedImages + file.getLastPathSegment());

                        mUploadTask = photoRef.putFile(file);
                        mUploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                                Uri downloadUrl = taskSnapshot.getDownloadUrl();
                                uploadedImages.add(downloadUrl);

                                HashMap userMap = new HashMap();
                                userMap.put("image1",uploadedImages.toString());
                                userMap.put("image2",uploadedImages.toString());
                                mRefre.child("product-images").setValue(userMap);
                            }
                        });    
                    }
                }
        }
    }
}

I am doing firebase project and trying to upload multiple images using imagepicker with a maximum of 3 images. The images are successfully uploaded to firebase storage but when I try to get the URL it saves the 3 images in one child Value. I am trying to save it in separate values and name them so I can retrieve them later.

How can I fix this?

Image

Hamu Damu
  • 11
  • 3
  • you need to modify this part: userMap.put("image1",uploadedImages.toString()); userMap.put("image2",uploadedImages.toString()); mRefre.child("product-images").setValue(userMap); in the way you want the values to be saved in FirebaseDB – Rahul Shukla Nov 15 '17 at 04:50
  • What does it mean, "it saves the 3 images in one child Value"? Give me an example. – Alex Mamo Nov 15 '17 at 08:26
  • Alex Mamo You can see my code its downloading the image Uri 3x in Child Image1 and child image2 i want to rename the selected images and save them as child and value the image url do you have solution – Hamu Damu Nov 15 '17 at 08:46

1 Answers1

0

I'd try a different approach: instead of looping, use the built in Promise/Task construct. This is detailed: https://gist.github.com/mcdonamp/d4f1327b58ad69334ef06327184df790 or How to upload multiple files to Firebase? and Android Firebase multiple image upload

Mike McDonald
  • 15,609
  • 2
  • 46
  • 49