File_Picker 插件不返回 Flutter 中写入文件的绝对路径

问题描述

我正在使用 file_picker 插件从我的 Flutter 应用程序的设备存储中挑选文件。我需要一个绝对路径,以便我可以读/写文件。但是使用file_picker插件选择文件只会返回存储在应用程序缓存中的复制文件的路径(文件加载并缓存在:/data/user/0/com.example.file_locker/cache/file_picker/Screenshot_2021-03-09 -00-59-58-834_com.linkedin.android.jpg ).

我正在使用 file_picker:^2.1.6

file_picker 存储库问题 here

现在,是否有任何解决方案可以从 file_picker 获取绝对路径,以便我可以读/写文件? 我被困在我的项目上,您的解决方案将不胜感激。

我见过其他文件浏览器插件,如果您知道其他一些返回所需解决方案的其他文件,它们也以相同的方式执行,请告诉我。

提前致谢。

解决方法

pickFile 的路径在path 属性中可用,作为插件页面示例中的状态:

FilePickerResult result = await FilePicker.platform.pickFiles();

if(result != null) {
   PlatformFile file = result.files.first;
   
   print(file.name);
   print(file.bytes);
   print(file.size);
   print(file.extension);
   print(file.path);
}

如果您的目标是将 pickFile 写入应用程序文档目录或临时目录中的文件,您可以使用 path_provider:https://pub.dev/packages/path_provider

// Get the application document directory
Directory appDocDir = await getApplicationDocumentsDirectory();
// Get the absolute path
String appDocPath = appDocDir.path;
// Copy it to the new file
final File newFile = await file.copy('$appDocPath/your_file_name.${file.extension}');
,
await FilePicker.platform.getDirectoryPath();

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...