在Android 12中,如果您尝试剪裁图像并使用file:// URI传递它,则可能会遇到SecurityException错误。这是因为Android 12以及更高版本不允许您的应用访问另一个应用的私有目录。
为了解决这个问题,您可以使用ContentResolver获取URI并从InputStream读取图像。以下是一个示例方法,它将获取图像URI并返回一个Bitmap:
public Bitmap getBitmap(Context context, Uri uri) throws IOException {
InputStream inputStream = context.getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
inputStream.close();
return bitmap;
}
这种方法避免了使用file:// URI并使用ContentResolver获取它,因此不会导致SecurityException错误。