A
A
Andrey Makarkin2018-05-08 17:53:36
Android
Andrey Makarkin, 2018-05-08 17:53:36

How to get multiple images from gallery?

Good day everyone. There were two questions.
1. You need to get multiple images from the gallery.
what we do (I will give only part of the code so as not to clog):
- we hang on the button:

Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType(IMAGE_TYPE);
                //intent.setAction(Intent.ACTION_GET_CONTENT);
                // this line is different here !!
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                startActivityForResult(Intent.createChooser(intent,
                        getString(R.string.no_task)), PICK_FROM_GALLARY);

- we announce:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

and in it we already get the uri and the path to the image:
if (data.getData() != null) {
                        try {
                            //Получаем URI изображения, преобразуем его в Bitmap
                            //объект и отображаем в элементе ImageView нашего интерфейса:
                            final Uri imageUri = data.getData();
                            final InputStream imageStream = getActivity().getContentResolver().openInputStream(imageUri);
                            final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                            imageView.setImageBitmap(selectedImage);
                            editTextScript.append(getPath(imageUri));


                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                    }

in this case, we perfectly get data for one selected image. how to handle if multiple images are selected?
2. question from here. as you can see at the beginning, I have a line commented out:
//intent.setAction(Intent.ACTION_GET_CONTENT);
the fact is that if I uncomment it, then I don’t get anything here:
editTextScript.append(getPath(imageUri));
editText is just empty.
while debugging is in progress, I display the path to the image in editText.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question