Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 833
Sahil Avatar asked Mar 02 '21 01:03

Sahil


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.


Video Answer


4 Answers

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.

like image 183
shirley Avatar answered Sep 24 '22 22:09

shirley


You need to change

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

to

File sd_directory = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

like image 41
Bhavesh Chand Avatar answered Oct 10 '22 16:10

Bhavesh Chand


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());
        }
    }
like image 29
Roshan kumar Avatar answered Oct 10 '22 15:10

Roshan kumar


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.

like image 8
zhangxaochen Avatar answered Oct 10 '22 16:10

zhangxaochen