简体   繁体   中英

Capture photo from camera is not working in Android 11

I used default camera of device for capture photo in my app. When I used android 10 and below version everything is working fine. but when I used camera in android 11 then not working in app. Can you help me to solve this problem.

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

You need to change

File sd_directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

to

File sd_directory = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

Add this to your manifest outside the application tag, I hope it will resolve your issue

<queries>
        <intent>
            <action android:name="android.media.action.IMAGE_CAPTURE" />
        </intent>
        <intent>
            <action android:name="android.intent.action.PICK" />
            <data android:mimeType="vnd.android.cursor.dir/image" />
        </intent>
</queries>

Android 11 has Storage update. So When using SDK >= 29 must set URI instead of File path.

URI uri = null;   // set this uri in camera Intent
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
           uri =  getApplicationContext().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
        }
        else
        {
            uri =   getApplicationContext().getContentResolver().insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, new ContentValues());
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM