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

相关内容

热门资讯

脚本详细!wpk德州局怎么透视... 脚本详细!wpk德州局怎么透视,wpk可以作必弊吗(透视)总是真的是有神器(哔哩哔哩)1、完成德州局...
脚本详细!hhpoker可以控... 脚本详细!hhpoker可以控制吗,hhpoker可以控制牌吗(透视)本来有脚本(哔哩哔哩)1、任何...
透视必备!德普之星怎么开辅助,... 透视必备!德普之星怎么开辅助,德普之星透视(透视)本来有软件(哔哩哔哩)透视方法中分为三种模型:靠谱...
透视关于!德普之星怎么设置埋牌... 透视关于!德普之星怎么设置埋牌,德普之星的辅助工具介绍(透视)总是是真的工具(哔哩哔哩);是不是有人...
透视详情!wepoker软件靠... 透视详情!wepoker软件靠谱么,wepoker透视底牌脚本(透视)确实真的有工具(哔哩哔哩)在进...
脚本推荐!wpk模拟器,wpk... 脚本推荐!wpk模拟器,wpk辅助软件(透视)都是真的是有教程(哔哩哔哩)1、脚本推荐!wpk模拟器...
透视解密!wpk俱乐部有没有辅... 透视解密!wpk俱乐部有没有辅助,如何下载wpk透视版(透视)确实真的有攻略(哔哩哔哩)1、下载好透...
透视推荐!德普之星有透视辅助吗... 透视推荐!德普之星有透视辅助吗,德扑圈有透视吗(透视)一直是有插件(哔哩哔哩)1、破解器简单,机器人...
透视开挂!hhpoker真的假... 透视开挂!hhpoker真的假的,hhpoker必备开挂(透视)一贯存在有方法(哔哩哔哩)能透视中分...
透视辅助!德普之星私人局透视,... 透视辅助!德普之星私人局透视,德普之星辅助工具如何打开(透视)竟然有插件(哔哩哔哩)1、辅助软件下载...