调用DocumentUI申请权限
Android R实现访问外部存储的Android/data方案

申请权限选择的目录树后,后续所以文件操作都需要按照此目录树为基础做文件操作

Uri的构造: “content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fdata/document/primary%3AAndroid%2Fdata”;

外部存储:content://com.android.externalstorage.documents
选择的目录树关键字:/tree/primary
选择的文件关键字:/document/primary

Uri uri = Uri.parse(“content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fdata/document/primary%3AAndroid%2Fdata”);

DocumentFile API

https://developer.android.com/reference/androidx/documentfile/provider/DocumentFile

DocumentFile API的使用

// 包名/file目录public static final String NOTE_BOOK_FILES_URI ="content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fdata/document/primary%3AAndroid%2Fdata%2Fcom.freeme.freemenote%2Ffiles";//判断文件是否存在 DocumentFile
Uri filesUri = Uri.parse(NOTE_BOOK_FILES_URI);
boolean isExistsFilesDir = DocumentFile.fromSingleUri(mContext, filesUri).exists();//不存在则创建 packageUri为父目录树
if (!isExistsFilesDir) {try {filesUri = DocumentsContract.createDocument(mContext.getContentResolver(), packageUri, "vnd.android.document/directory", "files");} catch (FileNotFoundException e) {e.printStackTrace();}
}//复制文件 File to DocumentFile DocumentsContract
public static void fileToDocumentFile(Context context, File originFile, String fileName, Uri parentUri) {//String fileName = originFile.getName();try {InputStream in = new FileInputStream(originFile);Uri documentFile = DocumentsContract.createDocument(context.getContentResolver(), parentUri, "*/*", fileName);//DocumentFile写入流OutputStream out = context.getContentResolver().openOutputStream(documentFile);byte[] buf = new byte[1024];int len;while ((len = in.read(buf)) > 0) {out.write(buf, 0, len);}in.close();out.close();} catch (Exception e) {e.printStackTrace();}
}// 读取DocumentFile to File
public static List<File> documentFileToFile(Context context) {List<File> allFile = new ArrayList<File>();Uri dirUri = Uri.parse(Constant.NOTE_BOOK_FILES_URI);DocumentFile documentFile = DocumentFile.fromTreeUri(context, dirUri);//遍历DocumentFileDocumentFile[] files = documentFile.listFiles();LogUtil.d(Constant.TAG, "documentFileToFile files count=" + files.length);for (DocumentFile file : files) {String fileName = file.getName();Uri fileUri = file.getUri();LogUtil.d(Constant.TAG, "documentFileToFile fileName=" + fileName + " fileUri=" + fileUri);try {//DocumentFile输入流InputStream in = context.getContentResolver().openInputStream(fileUri);File newFile = new File(Constant.BACKUP_DIR_PATH, fileName);OutputStream out = new FileOutputStream(newFile);byte[] buf = new byte[1024];int len;while ((len = in.read(buf)) > 0) {out.write(buf, 0, len);}in.close();out.close();allFile.add(newFile);} catch (Exception e) {e.printStackTrace();}}return allFile;
}

DocumentFile报错

Permission Denial: writing com.android.externalstorage.ExternalStorageProvider uri content:

原因:授权的Uri和使用时调用的Uri不匹配

Android DocumentFile基本使用相关推荐

  1. Android高版本使用DocumentFile读写外置存储的问题

    最近在维护项目,app遇到安装在高版本的Android时,以往直接授权和new File(path)的形式不再支持,日志也是说Permission denied.....好吧,换为DocumentFi ...

  2. [Android Pro] AndroidX重构和映射

    原文地址:https://developer.android.com/topic/libraries/support-library/refactor https://blog.csdn.net/ch ...

  3. android 5.0 模拟sd卡,如何使用为Android5.0(Lolliop)提供的新的SD卡访问API?

    在下面链接的我的Android项目中,您可以找到允许在Android 5中的ExtSDCard上编写的工作代码.它假设用户可以访问整个SD卡,然后允许您在这张卡上随时随地写东西.(如果您只想访问单个文 ...

  4. android androidx版本,Android AndroidX 简介与迁移

    AndroidX 简介 AndroidX 是 Android 团队用于在 Jetpack 中开发,测试,打包,版本和发布库的开源项目 . AndroidX 是对原始 Android Support L ...

  5. android 5.0 sd卡读写,如何使用为Android 5.0(Lollipop)提供的新SD卡存取API?

    很多好的问题,让我们深入.:) 你如何使用它? 这是一个伟大的教程与KitKat中的存储访问框架交互: 在Lollipop中与新的API交互非常相似.要提示用户选择目录树,您可以启动这样的意图: In ...

  6. Android 外置 SD 卡写入权限问题

    https://busy.im/post/android-sdcard-write/ 最近升级到 Android 9.0 后,发现文件管理器在写入外置 SD 卡时出现了写入失败的问题,定位到 File ...

  7. Android判断有无外置SD卡(TF卡),并读写文件

    转载来源:https://blog.csdn.net/qq_27061049/article/details/94178875 1.相关方法说明: 2.Android Manifest.xml 添加权 ...

  8. android免root读写u盘最新方法,支持安卓Q+

    android读写u盘支持安卓10以上的最新方法 本人是在写一个app需要读取u盘文件列表,网上找了好多方法,要不就是没有media权限,要不就是收不到广播,全部用不了,就这样搁置了一段时间终于又找到 ...

  9. Android存储访问框架的使用

    存储访问框架,简称:SAF, 就是系统文件选择器+文件操作API.先选择文件,在用文件操作API处理文件.系统文件选择器,就和Windows的文件选择框一样. 其实绝大多数app,都不会使用这个东西, ...

  10. Android SAF

    Android SAF 这里就不介绍SAF的相关背景,内部实现啥的了,相关知识网上一搜一大堆. 这里主要介绍SAF的相关操作. 别的程序和SAF之间的通信主要是通过Intent进行通信的.别的程序通过 ...

最新文章

  1. 信息安全系统设计基础期末总结
  2. 微软沈向洋:写给AI新潮流——人工智能创作的五点建议
  3. UVa 11168 Airport , 凸包
  4. 压缩base 64字符串_ftp下载多个文件,ftp下载多个文件打包成一个压缩包
  5. 【nginx运维基础(4)】Nginx的日志管理(日志格式与定时分割日志)
  6. leetcode944. 删列造序
  7. 华为云专属月·行业深耕专项行动正式开启
  8. 【hibernate merge】session1.merge(T entity)方法的含义和update方法的区别
  9. 华东理工计算机与金融专业,2019年华东理工大学各专业录取分数线
  10. php header函数的详解,php header函数的详解_PHP教程
  11. Android 系统(248)---解读Android进程优先级ADJ算法
  12. 虚拟化技术(1)——介绍
  13. 【资源】PyTorch版《动手学深度学习》开源了,最美DL书遇上最赞DL框架
  14. HDU 4966 GGS-DDU [最小树形图]
  15. u邮件收件服务器密码是什么,U-Mail邮件服务器加密技术给邮件上安全锁
  16. 《一个人的朝圣》读后感
  17. 第九届蓝桥杯稍小分数c语言,【第九届蓝桥杯大赛征文】蓝桥梦
  18. 引导魔女之力,征服星辰大海 主线篇: 重要事情说三遍: 推主线!推主线!!推主线!!! 简述: 1.本篇仅主线,涉及到的技巧全职业都可以参考; 2.考虑到萌新刚玩没有护卫者系统,故禁护
  19. 简单说说 RPC 框架,你 悟到了吗?
  20. 个人信息保护建设实践方法的探索过程

热门文章

  1. Package inputenc Error: Invalid UTF-8 byte “A1;Improper alphabetic constant. <to be read again>
  2. Qt--样式表设置按钮样式
  3. 已知树节点获取树的节点路径(js树节点路径)
  4. 兜兜转转,2020已过半
  5. 如何用后视镜判断车距
  6. 【转】深度技术分析“为什么ios比android流畅”
  7. LeetCode_回文数(三种解法-Java)
  8. python线程池ThreadPoolExecutor和as_completed的用法示例
  9. 暴力破解防范措施和措施总结
  10. linux 使用shell/python编写钉钉自动监控报警脚本