I am working with Camera Intent. Everything is working fine till Android 10, but in Android 11 I am getting result Code 0.
Manifest Permission
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Intent function with file creation :
private void openCameraApp()
{
Intent picIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE).
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
try {
String file_path = Environment.getExternalStorageDirectory().toString() +
"/" + mContext.getResources().getString(R.string.app_name);
File dir = new File(file_path);
if (!dir.exists())
dir.mkdirs();
imagePath = new File(dir, mContext.getResources().getString(R.string.app_name) + System.currentTimeMillis() + ".png");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
picIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID, imagePath));
setUri(FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID, imagePath));
} else {
picIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imagePath));
setUri(Uri.fromFile(imagePath));
}
((Activity) mContext).startActivityForResult(picIntent, CAMERA_REQUEST);
} catch (Exception e) {
logger.e(e);
}
}
I have added android:requestLegacyExternalStorage="true"
in application tag of manifest file.
To launch the camera, swipe the camera icon upwards.
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 .
See intent.resolveActivity returns null in API 30. Maybe there is something wrong with AndroidManifest.
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
Or see Cannot take a photo programmatically on Android 11 - intent returns canceled status.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With