AndroidQ(10)分区存储完美适配

前言

  • Android Q文件存储机制修改成了沙盒模式
  • APP只能访问自己目录下的文件和公共媒体文件
  • 对于AndroidQ以下,还是使用老的文件存储方式

背景

  • 存储权限
    Android Q仍然使用READ_EXTERNAL_STORAGE和WRITE_EXTERNAL_STORAGE作为存储相关运行时权限,但现在即使获取了这些权限,访问外部存储也受到了限制,只能访问自身目录下的文件和公共内体文件。
  • 外部存储结构划分
    1、公有目录:Downloads、Documents、Pictures 、DCIM、Movies、Music、Ringtones等
    地址:/storage/emulated/0/Downloads(Pictures)等
    公有目录下的文件不会跟随APP卸载而删除。
    2、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写入

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
  return null;
}
ContentResolver resolver = context.getContentResolver();
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");
//注意MediaStore.Downloads.RELATIVE_PATH需要targetVersion=29,
//故该方法只可在Android10的手机上执行
values.put(MediaStore.Downloads.RELATIVE_PATH, "Download" + File.separator + "apk");
Uri external = MediaStore.Downloads.EXTERNAL_CONTENT_URI;
Uri insertUri = resolver.insert(external, values);
return insertUri;

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

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

ContentResolver resolver = context.getContentResolver();
Uri insertUri = resolver.insert(external, values);
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读取公共目录下的文件

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删除文件

context.getContentResolver().delete(fileUri, null, null);

转载来自

会撒娇的犀犀利

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值