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

相关内容

热门资讯

发现玩家!广客麻将(透视)都是... 发现玩家!广客麻将(透视)都是是有挂(2024已更新)(哔哩哔哩)所有人都在同一条线上,像星星一样排...
微扑克有辅助挂!微扑克真的有辅... 微扑克有辅助挂!微扑克真的有辅助,wpk一贯是真的有挂,解说技巧(有挂ai代打)1、每个玩家都可以进...
wepokeai代打!wepo... wepokeai代打!wepoke软件透明挂,wepoke辅助真的假的,2025新版教程(有挂规律)...
必备辅助推荐!广东老友麻将有没... 必备辅助推荐!广东老友麻将有没有挂(透视)都是是有挂(2025已更新)(哔哩哔哩)1.广东老友麻将有...
微扑克有辅助挂!微扑克辅助钻石... 微扑克有辅助挂!微扑克辅助钻石,微扑克竟然是真的有挂,科技教程(有挂工具)是一款可以让一直输的玩家,...
wepoke有挂!德州wepo... wepoke有挂!德州wepower透视辅助,wepoke显示有,微扑克教程(有挂工具);原来确实真...
玩家爆料!东游麻将中挂件(辅助... 玩家爆料!东游麻将中挂件(辅助)一贯有挂(2024已更新)(哔哩哔哩);1、金币登录送、破产送、升级...
微扑克ai辅助!微扑克ai人工... 一、微扑克简介了解软件请加微:136704302微扑克是一款在线扑克游戏平台,玩家可以在平台上进行多...
透牌教程!wepoke游戏辅助... 透牌教程!wepoke游戏辅助工具(wepoke辅助挂)WepOke(一贯真的有挂);科技详细教程小...
分享实测!胡乐辅助器免费版(辅... 分享实测!胡乐辅助器免费版(辅助挂)竟然有挂(2025已更新)(哔哩哔哩)1、玩家可以在胡乐辅助器免...