繁体   English   中英

相机意图在 android 版本 11 中无法正常工作

[英]Camera intent is not working properly in android version 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, intentList, 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);
    }

当我将图像 bitmap 转换为 URI 时,我得到一个缩略图,所以这是一个模糊,所以我怎样才能在不使用 bitmap 的情况下获得 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 时才有效,当针对 Android 11 时,您必须使用 Scoped Storage ,从而实现对此功能的支持。 如果没有此实现,您将无法访问 Android 11+ 设备上的私有应用程序文件夹之外的文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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