简体   繁体   English

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

[英]File_Picker plugin doesn't return absolute path for write file in Flutter

I'm using the file_picker plugin to pick files from device storage in my flutter app.我正在使用 file_picker 插件从我的 flutter 应用程序中的设备存储中挑选文件。 I need an absolute path so that I can read/write files.我需要一个绝对路径,以便我可以读/写文件。 But picking file using file_picker plugin just returns the path of the copied file which is stored in the app cached (File loaded and cached at:/data/user/0/com.example.file_locker/cache/file_picker/Screenshot_2021-03-09-00-59-58-834_com.linkedin.android.jpg ).但是使用file_picker插件选择文件只返回存储在应用程序缓存中的复制文件的路径(文件加载和缓存在:/data/user/0/com.example.file_locker/cache/file_picker/Screenshot_2021-03-09 -00-59-58-834_com.linkedin.android.jpg)。

I'm using file_picker: ^2.1.6 .我正在使用file_picker: ^2.1.6

file_picker repository issue here . file_picker 存储库问题 在这里

Now, are there any solutions to get the absolute path from file_picker so that I can read/write the file?现在,是否有任何解决方案可以从 file_picker 获取绝对路径,以便我可以读/写文件? I'm stuck on my project, and your solutions will be appreciated.我被困在我的项目上,您的解决方案将不胜感激。

I've seen other file explorer plugin they also did the same way if you know some other else which return the desired solutions please let me know.我见过其他文件资源管理器插件,如果您知道其他返回所需解决方案的其他插件,它们也采用相同的方式,请告诉我。

Thank you in advance.先感谢您。

The path of the pickedFile is available in the path property as state in the example of the plugin page:在插件页面的示例中,pickFile 的路径在 path 属性中为 state :

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);
}

if your goal is to write the pickedFile to a file in the application documents directory or in the temporary directory, you could use path_provider for that: https://pub.dev/packages/path_provider如果您的目标是将 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();

I have also faced this issue,the problem is that new versions of file_picker does not provide the absolute path.我也遇到过这个问题,问题是新版本的 file_picker 没有提供绝对路径。 You can use the older versions of file_picker package.您可以使用旧版本的 file_picker package。 This works fine and gives you the absolute path by using the functions:这可以正常工作,并通过使用以下函数为您提供绝对路径:

  1. FilePicker.getMultiFilePath();
  2. FilePicker.getFilePath();

(Google these for more info.) (谷歌这些以获取更多信息。)

You need to disable null safety in your flutter to use these lower versions of the package.您需要在 flutter 中禁用 null 安全性才能使用这些较低版本的 package。

I have also faced this issue, file_picker doesn't show actual path for android, so i'm found solution for this case: "pick file and showing actual path".我也遇到过这个问题, file_picker没有显示 android 的实际路径,所以我找到了这种情况的解决方案:“选择文件并显示实际路径”。 i'using this filesystem_picker for open file by Directory我使用这个filesystem_picker按目录打开文件

         // for android Directory 
         Directory appDocDir = await Directory("storage/emulated/0");

         var result = await FilesystemPicker.open(allowedExtensions: [".png", ".jpg"],
         context: context,rootDirectory: appDocDir);
         if (result != null) {
             File file = File(result);
             print(file.parent.path)// the path where the file is saved 
         }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM