如何解决在范围存储 Android 11 中检索图像
我正在开发扫描仪应用。我的问题是从 Android 10 和 11 的范围存储中检索保存的图像。图像已成功保存,但我无法检索它。当我得到保存图像位图的路径是 null
。
这是在 Android 11 中保存图像的代码:
File dirDest = new File(Environment.DIRECTORY_PICTURES,context.getString(R.string.app_name));
Long date = System.currentTimeMillis();
String extension = ".jpg";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentResolver resolver = context.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Images.Media.DISPLAY_NAME,date + extension);
contentValues.put(MediaStore.Images.Media.MIME_TYPE,"image/" + extension);
contentValues.put(MediaStore.Images.Media.DATE_ADDED,date);
contentValues.put(MediaStore.Images.Media.DATE_MODIFIED,date);
contentValues.put(MediaStore.Images.Media.SIZE,bitmap.getByteCount());
contentValues.put(MediaStore.Images.Media.WIDTH,bitmap.getWidth());
contentValues.put(MediaStore.Images.Media.HEIGHT,bitmap.getHeight());
contentValues.put(MediaStore.Images.Media.RELATIVE_PATH,dirDest + File.separator);
Uri newImageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues);
FileOutputStream fos = (FileOutputStream) resolver.openOutputStream(Objects.requireNonNull(newImageUri));
callback.write(fos);
fos.flush();
fos.close();
return String.valueOf(dirDest) + date + extension;
这是从 Android 11 范围内的共享存储中检索保存图像的代码。我在这部分代码中遇到问题,位图是 null
。我没有得到问题的要点。最后的问题是FileNotFoundException
。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
String[] file = {MediaStore.Images.Media._ID,MediaStore.Images.Media.RELATIVE_PATH,MediaStore.Images.Media.DISPLAY_NAME,MediaStore.Images.Media.SIZE,MediaStore.Images.Media.MIME_TYPE,MediaStore.Images.Media.WIDTH,MediaStore.Images.Media.HEIGHT,MediaStore.Images.Media.DATE_MODIFIED};
if (uri != null) {
cursor = context.getContentResolver().query(uri,file,null,null);
if (cursor != null) {
try {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(file[0]);
File path = new File(uri.getPath());
bitmap = BitmapFactory.decodeFile(path.getAbsolutePath());
cursor.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。