AndroidQ与之前版本存储图像时字节数不同
创始人
2024-10-09 17:01:37
0

在Android Q中,应用程序使用MediaStore API或直接访问本地文件系统存储图像时,会发现字节数与之前版本不同。这是因为Android Q推出了Scoped Storage,将应用程序的文件访问限制在了自己的私有目录内。

如果您的应用程序需要在Android Q上存储图像,并且要在之前版本和Android Q上使用相同的代码,则需要在应用程序中实现Scoped Storage。以下是使用Scoped Storage在Android Q上存储图像的代码示例:

//获取存储图像的uri Uri imageUri = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { imageUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageId); } else { String imagePath = Environment.getExternalStorageDirectory().toString() + "/MyApp/Images/"; File file = new File(imagePath, imageName); imageUri = Uri.fromFile(file); }

//使用Uri存储图像 OutputStream outputStream = null; try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { ContentResolver contentResolver = getContentResolver(); ContentValues contentValues = new ContentValues(); contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, imageName); contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/MyApp"); imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues); if (imageUri != null) { outputStream = contentResolver.openOutputStream(imageUri); } } else { File file = new File(Environment.getExternalStorageDirectory().toString() + "/MyApp/Images", imageName); outputStream = new FileOutputStream(file); }

//将图像写入输出流
if (outputStream != null) {
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
    outputStream.flush();
    outputStream.close();
}

} catch (IOException e) { e.printStackTrace(); }

在Android Q及更高版本上,可以使用MediaStore API向MediaStore中添加

相关内容

热门资讯

最终!哈糖大菠萝怎么开挂(透视... 最终!哈糖大菠萝怎么开挂(透视)丽水都莱破解器(切实有辅助工具)-哔哩哔哩1、每一步都需要思考,不同...
透视攻略!竞技联盟破解版最新版... 透视攻略!竞技联盟破解版最新版(透视)pokemmo辅助脚本(辅助)本来是真的有脚本(哔哩哔哩)1、...
于此同时!wepoker透视辅... 于此同时!wepoker透视辅助下载(透视)玖游卡五星辅助(竟然是真的辅助工具)-哔哩哔哩1、下载好...
透视妙招!约局吧开挂(透视)哈... 透视妙招!约局吧开挂(透视)哈糖大菠萝开挂(辅助)总是真的有攻略(哔哩哔哩)1、让任何用户在无需哈糖...
透视科技!we-poker正规... 透视科技!we-poker正规吗(透视)微信小程序微乐辅助器教程(总是存在有辅助插件)-哔哩哔哩1、...
透视步骤!德州来玩辅助器(透视... 透视步骤!德州来玩辅助器(透视)pokemmo手机版脚本(辅助)原来真的有app(哔哩哔哩)1、po...
透视ai代打!如何下载德普之星... 透视ai代打!如何下载德普之星辅助软件(透视)微信小程序万能修改器(原来是真的辅助插件)-哔哩哔哩微...
透视模块!德州局透视脚本免费版... 透视模块!德州局透视脚本免费版下载手机版(透视)uupoker透视(辅助)一贯真的是有脚本(哔哩哔哩...
目前!wepoker私人局辅助... 目前!wepoker私人局辅助挂(透视)天酷大厅辅助(切实有辅助工具)-哔哩哔哩天酷大厅辅助是不是有...
透视攻略!来玩app破解(透视... 透视攻略!来玩app破解(透视)约局吧怎么看有没有挂(辅助)切实真的有神器(哔哩哔哩)1、玩家可以在...