0

How can I change the code below to allow multiple images selection from the gallery as I am unsure which option to select as unable to get it working. The code below works for single image selection:

public void openGallery() {
    Intent intentImageContent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intentImageContent, loadImageResults);
}

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

    if (requestCode == loadImageResults) {
        if (resultCode == RESULT_OK && data != null) {
            Intent intent = new Intent(PhotosActivity.this, PhotosActivity.class);
            intent.putExtra("pickImage", data.getData());
            startActivity(intent);
        }
    }
}
mmkp
  • 145
  • 2
  • 16
  • May this will be useful for you [Answer by Kyle Shank](https://stackoverflow.com/a/19848052/6344335) – TejpalBh Jun 22 '18 at 12:19

2 Answers2

1

replace your code with this

   public void openGallery() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,"Select Picture"), 1);
}

And change onActivityResult with this

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    try {
        // When an Image is picked
        if (requestCode == 1 && resultCode == RESULT_OK
                    && null != data) {
            // Get the Image from data

            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            imagesEncodedList = new ArrayList<String>();
            if(data.getData()!=null){

                Uri mImageUri=data.getData();

                // Get the cursor
                Cursor cursor = getContentResolver().query(mImageUri,
                            filePathColumn, null, null, null);
                // Move to first row
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imageEncoded  = cursor.getString(columnIndex);
                cursor.close();

            }
        } else {
            Toast.makeText(this, "You haven't picked Image",
                        Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                    .show();
    }

    super.onActivityResult(requestCode, resultCode, data);
}
Milan Pansuriya
  • 2,521
  • 1
  • 19
  • 33
0

Try this

    Intent intentImageContent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    intentImageContent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
    startActivityForResult(intentImageContent, loadImageResults);
Hardik Vasani
  • 876
  • 1
  • 8
  • 14