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.
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.
You need to change
File sd_directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
to
File sd_directory = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
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());
}
}
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>