• Android DocumentFile基本使用


    调用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);
        //遍历DocumentFile
        DocumentFile[] 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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    DocumentFile报错

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

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

  • 相关阅读:
    Grafana+Prometheus打造运维监控系统(一)-安装篇
    将时间序列转成图像——希尔伯特-黄变换方法 Matlab实现
    iOS开发:Mach-O入门理解
    【React 钩子函数 useMemo以及useMemo和useEffect的区别】
    照片转换成3D软件有什么?建议收藏这些软件
    kafka命令行操作
    与目前主流的消费返利平台对比,共享购模式有什么优势呢?
    Camunda 7.x 系列【48】候选用户和用户组
    如何将腾讯文档(在线文档)转换成二维码?
    java计算机毕业设计ssm养老管理系统-敬老院系统
  • 原文地址:https://blog.csdn.net/weixin_44008788/article/details/126065952