Android 11 Capture image using Camera

Viewed 12048

Documentation - https://developer.android.com/training/camera/photobasics

I have followed all the required steps to capture image using camera.

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) { // its always null
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

takePictureIntent.resolveActivity(getPackageManager()) - this line always returns null. and if i skip this check than camera opens but app crashes.

2 Answers

Jaakko's answer is correct, and here is a quick explanation:

Code:

<manifest package="your.package.name">
<queries>
    <intent>
        <action android:name="android.media.action.IMAGE_CAPTURE" />
    </intent>
</queries>
</manifest>

This works only for the default camera apps. If your app is using some 3rd party camera, you can find some info here: https://commonsware.com/blog/2020/08/16/action-image-capture-android-r.html

Add these to AndroidManifest.xml inside the manifest section:

<queries>
  <intent>
    <action android:name="android.media.action.IMAGE_CAPTURE" />
  </intent>
</queries>
Related