简体   繁体   中英

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. 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 ).

I'm using file_picker: ^2.1.6 .

file_picker repository issue here .

Now, are there any solutions to get the absolute path from file_picker so that I can read/write the file? 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:

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

// 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. You can use the older versions of 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.

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". i'using this filesystem_picker for open file by Directory

         // 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 
         }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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