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.
・ Android 6.0.1
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.
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 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);
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.
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