0

I've successfully created an intent chooser which lists Gallery, Documents and Camera. But when I try to select Camera from the chooser, nothing happens on clicking it.
I tried creating a chooser with just Camera Intent, even then the result was the same.

This exact code was working before the Marshmallow update. I suspect it has something to do with it. Have you come across a similar problem? And How did you solve it?

Following is my code to create an intent chooser.

public Intent getPickImageChooserIntent() {

    // Determine Uri of camera image to save.
    fileUri = getCaptureImageOutputUri();

    List<Intent> allIntents = new ArrayList<>();
    PackageManager packageManager = getPackageManager();

    // collect all camera intents
    Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for (ResolveInfo res : listCam) {
        Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(res.activityInfo.packageName);
        if (fileUri != null) {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        }
        allIntents.add(intent);
    }

    // collect all gallery intents
    Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
    galleryIntent.setType("image/*");
    List<ResolveInfo> listGallery = packageManager.queryIntentActivities(galleryIntent, 0);
    for (ResolveInfo res : listGallery) {
        Intent intent = new Intent(galleryIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(res.activityInfo.packageName);
        allIntents.add(intent);
    }

    // the main intent is the last in the list (fucking android) so pickup the useless one
    Intent mainIntent = allIntents.get(allIntents.size() - 1);
    for (Intent intent : allIntents) {
        if (intent.getComponent().getClassName().equals("com.android.documentsui.DocumentsActivity")) {
            mainIntent = intent;
            break;
        }
    }
    allIntents.remove(mainIntent);

    // Create a chooser from the main intent
    Intent chooserIntent = Intent.createChooser(mainIntent, "Select source");

    // Add all other intents
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, allIntents.toArray(new Parcelable[allIntents.size()]));

    return chooserIntent;
}
fadden
  • 51,356
  • 5
  • 116
  • 166
Kshitij
  • 639
  • 1
  • 10
  • 25
  • 2
    [New permission model ...](https://developer.android.com/training/permissions/requesting.html) – tynn Nov 02 '15 at 10:08
  • Thanks @tynn for pointing me in the right direction. This indeed is because of runtime permission model. – Kshitij Nov 02 '15 at 10:25

1 Answers1

0

As @Tynn pointed out, The problem is because of the new Permission Model. I had to setup runtime checks for the permission status for each of the permissions.

Kshitij
  • 639
  • 1
  • 10
  • 25