前言

最近时间在做AndroidQ的适配,截止到今天AndroidQ分区存储适配完成,期间出现很多坑,目前网上的帖子大部分都是概述变更内容,接下来的几篇帖子都是对分区存储实际经验代码总结,填坑经验,特此记录一下,也为大家提供帮助。

相关系列文章

本篇主要是对AndroidQ(10)分区存储适配具体实现

要点:

Android Q文件存储机制修改成了沙盒模式

APP只能访问自己目录下的文件和公共媒体文件

对于AndroidQ以下,还是使用老的文件存储方式

这里需要注意:在适配AndroidQ的时候还要兼容Q系统版本以下的,使用SDK_VERSION区分

背景

存储权限

Android Q仍然使用READ_EXTERNAL_STORAGE和WRITE_EXTERNAL_STORAGE作为存储相关运行时权限,但现在即使获取了这些权限,访问外部存储也受到了限制,只能访问自身目录下的文件和公共内体文件。

header 1

无权限

READ_EXTERNAL

Audio

可读写APP自己创建的文件,但不可直接使用路径访问

可以读其他APP创建的媒体类文件,删改操作需要用户授权

Image

可读写APP自己创建的文件,但不可直接使用路径访问

可以读其他APP创建的媒体类文件,删改操作需要用户授权

File

可读写APP自己创建的文件,但不可直接使用路径访问

不可读写其他APP创建的非媒体类文件

Downloads

可读写APP自己创建的文件,但不可直接使用路径访问

不可读写其他APP创建的非媒体类文件

外部存储结构划分

公有目录:Downloads、Documents、Pictures 、DCIM、Movies、Music、Ringtones等

地址:/storage/emulated/0/Downloads(Pictures)等

公有目录下的文件不会跟随APP卸载而删除。

APP私有目录

地址:/storage/emulated/0/Android/data/包名/files

私有目录存放app的私有文件,会随着App的卸载而删除。

适配指导

AndroidQ中使用ContentResolver进行文件的增删改查

1、获取(创建)自身目录下的文件夹

获取及创建,如果手机中没有对应的文件夹,则系统会自动生成

//在自身目录下创建apk文件夹
File apkFile = context.getExternalFilesDir("apk");

2、创建自身目录下的文件

生成需要下载的路径,通过输入输出流读取写入

String apkFilePath = context.getExternalFilesDir("apk").getAbsolutePath();
File newFile = new File(apkFilePath + File.separator + "temp.apk");
OutputStream os = null;
try {
os = new FileOutputStream(newFile);
if (os != null) {
os.write("file is created".getBytes(StandardCharsets.UTF_8));
os.flush();
}
} catch (IOException e) {
} finally {
try {
if (os != null) {
os.close();
}
} catch (IOException e1) {
}
}

3、创建并获取公共目录下的文件路径

通过MediaStore.insert写入

//这里的fileName指文件名,不包含路径
//relativePath 包含某个媒体下的子路径
private static Uri insertFileIntoMediaStore (String fileName, String fileType,String relativePath) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
return null;
}
ContentResolver resolver = context.getContentResolver();
//设置文件参数到ContentValues中
ContentValues values = new ContentValues();
//设置文件名
values.put(MediaStore.Downloads.DISPLAY_NAME, fileName);
//设置文件描述,这里以文件名为例子
values.put(MediaStore.Downloads.DESCRIPTION, fileName);
//设置文件类型
values.put(MediaStore.Downloads.MIME_TYPE,"application/vnd.android.package-archive");
//注意RELATIVE_PATH需要targetVersion=29
//故该方法只可在Android10的手机上执行
values.put(MediaStore.Downloads.RELATIVE_PATH, relativePath);
//EXTERNAL_CONTENT_URI代表外部存储器
Uri external = MediaStore.Downloads.EXTERNAL_CONTENT_URI;
//insertUri表示文件保存的uri路径
Uri insertUri = resolver.insert(external, values);
return insertUri;
}

4、公共目录下的指定文件夹下创建文件

结合上面代码,我们主要是在公共目录下创建文件或文件夹拿到本地路径uri,不同的Uri,可以保存到不同的公共目录中。接下来使用输入输出流就可以写入文件

下面代码仅是以文件复制存储举例,`sourcePath`表示原文件地址,复制文件到新的目录; 文件下载直接下载本地,无需文件复制。

重点:AndroidQ中不支持file://类型访问文件,只能通过uri方式访问

private static void saveFile(Context context, Uri insertUri){
if(insertUri == null) {
return;
}
String mFilePath = insertUri.toString();
InputStream is = null;
OutputStream os = null;
try {
os = resolver.openOutputStream(insertUri);
if(os == null){
return;
}
int read;
File sourceFile = new File(sourcePath);
if (sourceFile.exists()) { // 文件存在时
is = new FileInputStream(sourceFile); // 读入原文件
byte[] buffer = new byte[1024];
while ((read = is.read(buffer)) != -1) {
os.write(buffer, 0, read);
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

5、使用MediaStore读取公共目录下的文件

通过ContentResolver openFileDescriptor接口,选择对应的打开方式。

例如”r”表示读,”w”表示写,返回ParcelFileDescriptor类型的文件描述符。

private static Bitmap readUriToBitmap (Context context, Uri uri) {
ParcelFileDescriptor parcelFileDescriptor = null;
FileDescriptor fileDescriptor = null;
Bitmap tagBitmap = null;
try {
parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r");
if (parcelFileDescriptor != null && parcelFileDescriptor.getFileDescriptor() != null) {
fileDescriptor = parcelFileDescriptor.getFileDescriptor();
//转换uri为bitmap类型
tagBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
// 你可以做的~~
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (parcelFileDescriptor != null) {
parcelFileDescriptor.close();
}
} catch (IOException e) {
}
}
}

6、使用MediaStore删除文件

public static void deleteFile (Context context, Uri fileUri) {
context.getContentResolver().delete(fileUri, null, null);
}

后续对AndroidQ存储针对具体功能做介绍,欢迎关注~

大佬点个赞再走呗~