android - 将文件转换为 DocumentFile
问题描述
我想将File
( /storage/A54E-14E9/bp.mp4
) 转换为DocumentFile
Uri
的DocumentFile
应该是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
解决方案
推荐阅读
- r - 按列元素的条件分组
- javascript - 未在循环中定义车把参数
- amazon-web-services - 使用 Terraforms 文件配置器上传文件夹
- npm - NPM 包安装受影响的依赖列表
- google-sheets - 从 Google 表格单元格的第一个括号中提取
- c# - 在 sp_UpdateStockItemAmount 上出现 inteccorect 语法错误
- javascript - 调用signInCallback()时反应应用程序oidc-client IE11问题
- c++ - constexpr 与 lambda 表达式的 C++ 用法
- amazon-web-services - 如何在 AWS 中监控通过我的 VPN 的每个请求?
- javascript - 使用 redux-persist 保留某些属性不起作用