I'm trying to launch an intent to pick a image from the camera or the android's gallery. I checked THIS post and currently my code is near to work:
private Intent getPickIntent() {
final List<Intent> intents = new ArrayList<Intent>();
if (allowCamera) {
setCameraIntents(intents, cameraOutputUri);
}
if (allowGallery) {
intents.add(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI));
}
if (intents.isEmpty()) return null;
Intent result = Intent.createChooser(intents.remove(0), null);
if (!intents.isEmpty()) {
result.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toArray(new Parcelable[] {}));
}
return result;
}
private void setCameraIntents(List<Intent> cameraIntents, Uri output) {
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = context.getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, output);
cameraIntents.add(intent);
}
}
When I set allowCamera=true
it works correctly.
When I set allowGallery=true
it shows the following chooser:
But if I set allowCamera=true
and allowGallery =true
the chooser shown is:
And if you select Android System
then the first chooser is shown.
I'd like the chooser to be something like:
How can I "expand" the Android System
option?
In your linked post you can find the solution. The difference to your code is how the gallery intent is created:
Not with
ACTION_PICK
how you did but withACTION_GET_CONTENT
. It seems that if a singleACTION_PICK
is in the list (a "container intent"), the system traverses it to display the content of the pick, but as soon you include the camera intent it can't traverse anymore (since there is one direct intent and one container intent).In the comment of this answer you find the difference between
ACTION_PICK
andACTION_GET_CONTENT
.There are some solutions available which recommend to use a custom dialog. But this dialog will not have the standards icons (see develper docs here). So I recommend to stay at your solution and just fix the hierarchie issue.
Intent for showing camera and video files in activity chooser:
Your code already very achieve the goal. Just exchange the gallery and camera add order.
Here's my test result. Appearance difference since my OS is Android Nougat(7.0)
The first screenshot is in your order, the second is after exchange. Just like you said, in first situation, click the system logo will show gallery items.
You may need to create a custom dialog for this:
Here is the code for the method which is to be called when the user clicks on a particular button:
And the code for handling
onActivityResult()
:EDIT: Try Using this: