[JAVA] How to narrow down the image format from the gallery on Android and then select and import multiple images

I decided to develop an Android application at work, and I made an image capture function using something called Storage Access Framework, but it is a memorandum because there are few Japanese materials and it took time. I'm sorry if there is a mistake because it is the first Android development and there is a period from development to summarizing the contents.

environment

・ Android 6.0.1

How to call the gallery

On Android, it seems that intent is used when calling various functions of other apps and Android standard.

Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 0);

By setting "image / *" in setType, only images can be imported.

0 is set in the second argument of startActivityForResult, but here we set something called requestCode. This is used to determine which app or function the call is from, and will be used in the return process described below. Any value can be used as long as it is an int, but when implementing it, it is better to make it a constant so that the same value can be used for calling and returning.

Multiple selection

I don't like to select one by one, so I will allow you to select multiple images.

Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, 0);

If you don't want to call extra apps

If you keep it above, you can also select files from network drives and other apps. In this case, it is annoying to touch on extra functions when creating the operation procedure manual, so delete it.

Intent intent = new Intent();
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, 0);

Select image format

I want to limit the image formats that can be handled, so set the image formats that can be selected.

When there is one format (example is JPEG)

Intent intent = new Intent();
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, 0);

Two or more (examples are JPEG and PNG)

Intent intent = new Intent();
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, new String[] {"image/jpeg", "image/png"});
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, 0);

The call looks like this.

Handling of returns from the gallery

From here, it is about the processing on the receiving side. If you write it in the mainActivity, it will work.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            Uri = uri;
            if (data.getData() != null) {
                //Processing when the selected image is singular
                uri = data.getData();
            } else {
                //Processing when there are multiple selected images
                ClipData cd = data.getClipData();
                for (int i=0; i<cd.getItemCount(); i++) {
                    uri = cd.getItemAt(i).getUri();
                }
            }
        }
    }
}

The requestCode will be the one set by the caller's startActivityForResult (). If there is a return from multiple functions, change the processing for each function with if or swicht.

resultCode is the success or failure of the call.

Now you can get the URI of the selected image, so feel free to do the rest.

Recommended Posts

How to narrow down the image format from the gallery on Android and then select and import multiple images
Android app to select and display images from the gallery
[Android] How to turn the Notification panel on and off using StatusBarManager
[Android] Display images and characters on the ViewPager tab
[Android] Uploading images from your device to the server
[Ruby On Rails] How to search and save the data of the parent table from the child table
How to use OpenCV 4 on Android and view camera live view
Hold down the practical and essential images to tackle TDD
How to create a form to select a date from the calendar
How to disable Set-Cookie from API on the front side
How to run React and Rails on the same server
Zip and upload multiple files to Firebase Storage on Android.
How to apply C code format from the command line
How to install and configure the monitoring tool "Graphite" on Ubuntu
Upload multiple images to Cloudinary on Active Storage and publish to Heroku
[Rails] Don't use the select method just to narrow down the columns!
How to place and share SwiftLint config files on the server
[Java] How to convert from String to Path type and get the path
How to "hollow" View on Android
[Java] How to retrieve the parameters passed from html on the server side
[Java] How to get the current date and time and specify the display format
[Android] How to pass images and receive callbacks when sharing using ShareCompat