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中添加

相关内容

热门资讯

玩家必备教程!wepoker游... 玩家必备教程!wepoker游戏安装教程(辅助挂)确实真的是有挂(有挂规律)1、首先打开wepoke...
实测必看!wepoker透视脚... 实测必看!wepoker透视脚本免费(辅助挂)总是真的有挂(有挂秘笈)1.wepoker透视脚本免费...
教程攻略!竞技联盟辅助插件(辅... 教程攻略!竞技联盟辅助插件(辅助挂)好像是有挂(果真有挂)1、下载好竞技联盟辅助插件脚本下载之后点击...
技术分享!xpoker透视辅助... 技术分享!xpoker透视辅助(辅助挂)真是有挂(有挂存在)一、xpoker透视辅助可以开透视的定义...
玩家必看科普!wejoker辅... 玩家必看科普!wejoker辅助软件(辅助挂)都是有挂(真实有挂)1、进入到wejoker辅助软件是...
一分钟了解!uupoker有透... 一分钟了解!uupoker有透视吗(辅助挂)真是有挂(有挂技术)1、进入游戏-大厅左侧-新手福利-激...
玩家必看分享!hhpoker真... 玩家必看分享!hhpoker真的假的(辅助挂)真是有挂(有挂助手)1、玩家可以在hhpoker真的假...
分享给玩家!wepoker透视... 分享给玩家!wepoker透视有用吗(辅助挂)本来是真的有挂(详细教程)1、wepoker透视有用吗...
总算了解!aapoker辅助工... 总算了解!aapoker辅助工具安全吗(辅助挂)其实存在有挂(确实有挂)1、超多福利:超高返利,海量...
发现玩家!德普之星辅助软件(辅... 发现玩家!德普之星辅助软件(辅助挂)竟然真的有挂(有挂方式)1、用户打开应用后不用登录就可以直接使用...