首页 > 解决方案 > 将文件转换为 DocumentFile

问题描述

我想将File( /storage/A54E-14E9/bp.mp4) 转换为DocumentFile

UriDocumentFile应该是content://com.android.externalstorage.documents/tree/A54E-14E9%3A/document/A54E-14E9%3Abp.mp4

但是当我使用DocumentFile.fromFile(file).getUri()它时返回不正确Uri

file:///storage/A54E-14E9/bp.mp4

onActivityResult所以它和你打电话后进去的时候不同startActivityForResult(new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE), REQUEST_CODE_STORAGE_ACCESS);

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_STORAGE_ACCESS && resultCode == RESULT_OK) {
            Uri treeUri = data.getData();
            mDocumentDir = DocumentFile.fromTreeUri(this, treeUri);
            for (DocumentFile file : mDocumentDir.listFiles()) {
                // one of them is content://com.android.externalstorage.documents/tree/A54E-14E9%3A/document/A54E-14E9%3Abp.mp4
                Log.i(TAG, file.getUri());
            }
        }
    }

或者如果不可能,那么如何在以下位置找到等于File( /storage/A54E-14E9/bp.mp4) 的内容:

// in onActivityResult
for (DocumentFile file : mDocumentDir.listFiles()) {
    Log.i(TAG, file.getUri());
}

只检查文件名不是好主意,我需要检查绝对路径(如果它们相等),文件也可以放在子文件夹中,所以这让事情变得更加困难

File我的任务是从MicroSD 卡中删除,它可以DocumentFile在请求存储后使用(ACTION_OPEN_DOCUMENT_TREE)但我需要以某种方式获得与DocumentFile我相同的权利File

标签: androidandroid-filedocumentfile

解决方案


推荐阅读