0

In my application I want to select multiple images from gallery and need to upload those images on server.I my code now only single image is selected below is my code please give me solution :-

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


                Intent intent = new Intent();
// Show only images, no videos or anything else
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

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

        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

            Uri uri = data.getData();

            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                // Log.d(TAG, String.valueOf(bitmap));

//                ImageView imageView = (ImageView) findViewById(R.id.imageView);
//                imageView.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

2 Answers2

3

There is an extra Allow Multiple option in intent

intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

In your code

Intent intent = new Intent();
// Show only images, no videos or anything else
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
// Always show the chooser (if there are multiple options available)
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

Then in your activityResult

ClipData clipData = data.getClipData();

And then iterate clipData to get paths

for (int i = 0; i < clipData.getItemCount(); i++)

{
    Uri uri = clipData.getItemAt(i).getUri();
}

Hope this helps

Abdul Kawee
  • 2,687
  • 1
  • 14
  • 26
  • @SagarDeshmukh if it helped you dont forget to accept and rate up the answer so that it may help others in future :) – Abdul Kawee May 20 '17 at 05:55
0

Use this Library, With this You can select multiple images as well as you can directly capture from camera. It is a very simple library to integrate. Hope this will help.TedBottomPicker

Ashish Gupta
  • 737
  • 11
  • 18