4

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.

Nayeem Islam
  • 51
  • 1
  • 7

4 Answers4

3

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 
         }
mamena tech
  • 496
  • 5
  • 16
0

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}');
Arnaud Delubac
  • 759
  • 6
  • 13
  • My goal is to pick files and copy their path, then delete them from their current location of the device storage. In your suggestions, I can do this for a copy but it also necessary for me to delete it from its original location. could you please provide any solutions for achieving this? @Arnaud – Nayeem Islam Mar 09 '21 at 17:27
  • i am not 100% sure, but i think that you can't do that, you can only write/delete inside the application document folder – Arnaud Delubac Mar 09 '21 at 18:01
  • Yes! we can do it in our application document folder. I wish there any other way to do that to resolve my problem. Btw thank you for your response. – Nayeem Islam Mar 10 '21 at 04:37
  • @NayeemIslam just say you want the path and stop there. he only answered the question he can – stackunderflow Jun 25 '22 at 15:41
  • we just want the file path from file picker. why were you bother suggesting other package that won't solve anything? – stackunderflow Jun 25 '22 at 15:43
  • 1
    @stackunderflow I've used the method channel and got the absolute path that I wanted. This way I came to a solution and in my opinion, the file picker is not able to do that. – Nayeem Islam Jul 02 '22 at 09:12
0
await FilePicker.platform.getDirectoryPath();
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Aashak Rs
  • 1
  • 2
0

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.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Sriraj
  • 1