Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 11 Capture image using Camera

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.

like image 637
towhid Avatar asked Sep 18 '20 06:09

towhid


People also ask

Does image capture work with Android?

Open Image Capture. In the sidebar, click on your Android device. Choose the folder where you want to save your pictures using the drop-down menu. Then, select the images you want to transfer and click Download.

Which intent action do you use to take a picture with a camera app?

The Android Camera application encodes the photo in the return Intent delivered to onActivityResult() as a small Bitmap in the extras, under the key "data" . The following code retrieves this image and displays it in an ImageView . Note: This thumbnail image from "data" might be good for an icon, but not a lot more.


1 Answers

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

  • Since API level 30, there have been changes in the package visibility. https://developer.android.com/about/versions/11/privacy/package-visibility

  • For your package manager to work properly, you need to declare <queries> in your AndroidManifest.xml:

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

like image 54
Pavle37 Avatar answered Oct 03 '22 21:10

Pavle37