如何解决相机意图在 android 版本 11 中无法正常工作
I have try some following solution to captured image on android version 11. But this solution are not working. when I use bitmap that time I get blur image this Is not visible properly.I have added the below code in the manifest.
android:requestLegacyExternalStorage="true" ` Add this top stored image in external storage`
<queries>
<intent>`
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
// add code in class call image Intent
public static Intent getPickImageIntent(Context context) {
mContext = context;
Intent chooserIntent = null;
List<Intent> intentList = new ArrayList<>();
Intent pickIntent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// takePhotoIntent.putExtra("return-data",true);
// takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(getTempFile(context)));
intentList = addIntentsToList(context,intentList,pickIntent);
intentList = addIntentsToList(context,takePhotoIntent);
if (intentList.size() > 0) {
chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1),context.getString(R.string.pick_image_intent_text));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,intentList.toArray(new Parcelable[]{}));
}
return chooserIntent;
}
when I add temp file path then this is not working in above API level 30
// pass image uri to activity set image in imageview
public static Uri getimageFromresultUri(Context context,int resultCode,Intent imageReturnedIntent) {
File imageFile = getTempFile(context);
Uri selectedImage = null;
int sdkVersion = Build.VERSION.SDK_INT;
if (resultCode == Activity.RESULT_OK) {
boolean isCamera = (imageReturnedIntent == null ||
imageReturnedIntent.getData() == null ||
imageReturnedIntent.getData().toString().contains(imageFile.toString()));
if (isCamera) { /** CAMERA **/
// selectedImage = Uri.fromFile(imageFile);
Bitmap photo = (Bitmap) imageReturnedIntent.getExtras().get("data");
selectedImage = getimageUri(context,photo);
} else { /** ALBUM **/
selectedImage = imageReturnedIntent.getData();
}
}
return selectedImage;
}
when I convert Bitmap image to URI
public static Uri getimageUri(Context mContext,Bitmap inImage){
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG,100,bytes);
String path = MediaStore.Images.Media.insertimage(mContext.getContentResolver(),inImage,"Title",null);
return Uri.parse(path);
}
当我将图像位图转换为 URI 时,我得到一个缩略图,所以这是一个模糊,所以如何在不使用位图的情况下在 android 版本 11 中获取图像。而且我不知道要在画廊中存储此图像。每个设备中的图像都变得模糊。 当我使用 takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(getTempFile(context)));这段代码然后它在低于版本 11 中正常工作。但是我如何为 android 版本 11 使用相同的代码
解决方法
Uri.fromFile(getTempFile(context)));
您应该使用 FileProvider 和 FileProvider.getUriForFile() 来为相机意图提供有效的 uri。
也适用于 Android 11 以下的版本;
,您的目标 API 版本是哪个? requestLegacyExternalStorage
仅在针对最多 Android 10 时有效,当 targetting Android 11 you have to use Scoped Storage 时,因此实现对此功能的支持。如果没有此实现,您将无法在 Android 11+ 设备上访问私有应用文件夹之外的文件
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。