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

相关内容

热门资讯

详情透视!hhpoker有透视... 详情透视!hhpoker有透视功能吗,手机微信小程序游戏破解器,大纲教程(有挂神器)-哔哩哔哩1、下...
分享透视!wepoker辅助分... 分享透视!wepoker辅助分析器,微乐四川亲友圈辅助器,模块教程(有挂透明挂)-哔哩哔哩1、操作简...
了解透视!wpk辅助是什么,手... 了解透视!wpk辅助是什么,手机微乐小程序自建房透视免费,策略教程(存在有挂)-哔哩哔哩1)wpk辅...
关于透视!wepoker免费脚... 关于透视!wepoker免费脚本弱密码,手机微乐小程序自建房透视免费,法门教程(有挂分享)-哔哩哔哩...
详细透视!大菠萝辅助器,微乐家... 详细透视!大菠萝辅助器,微乐家乡游戏攻略,窍门教程(有挂方法)-哔哩哔哩暗藏猫腻,小编详细说明大菠萝...
揭幕透视!黑侠破解wepoke... 揭幕透视!黑侠破解wepoker,微乐自建房怎么挂脚本,讲义教程(发现有挂)-哔哩哔哩1、进入到黑侠...
专业透视!wepoker挂,微... 专业透视!wepoker挂,微信小程序微乐破解器2025,妙计教程(存在有挂)-哔哩哔哩该软件可以轻...
辅助透视!智星德州插件2024... 辅助透视!智星德州插件2024最新版,微信小程序微乐辅助器下载,技法教程(有人有挂)-哔哩哔哩1、全...
必备透视!hhpoker开挂教... 必备透视!hhpoker开挂教程,微乐江苏小程序游戏破解下载,模板教程(有挂辅助)-哔哩哔哩hhpo...
普及透视!aa poker辅助... 您好,aa poker辅助这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很...