15

I'm making an Android app, and when using the image picker plugin;

final pickedFile = await ImagePicker().getImage(source: ImageSource.gallery);
File image = File(pickedFile.path);

the 'image' is a copy of the original image in the app's cache, however, I would like to directly use the original image's path to save it in my app because I don't want the apps cache size to grow. I saw that the deprecated method "pickImage" accomplished this (not copying to cache), but the new "getImage" seems to copy automatically and 'image's path is the path of the cached image.

How can I accomplish getting just the original path of the selected image without it being cached? (I'm assuming that using the original file's path would still work to display it in the app with FileImage(File(originalPath)), this is correct assumption?)

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
Milky
  • 153
  • 1
  • 5

2 Answers2

1

On iOS it was never possible to retrieve the original path and on Android it was only possible until SDK 30.

From FAQ of file_picker plugin,

Original paths were possible until file_picker 2.0.0 on Android, however, in iOS they were never possible at all since iOS wants you to make a cached copy and work on it. But, 2.0.0 introduced scoped storage support (Android 10) and with and per Android doc recommendations, files should be accessed in two ways:

  • Pick files for CRUD operations (read, delete, edit) through files URI and use it directly — this is what you actually want but unfortunately isn’t supported by Flutter as it needs an absolute path to open a File descriptor;
  • Cache the file temporarily for upload or similar, or just copy into your app’s persistent storage so you can later access it — this is what’s being done currently and even though you may have an additional step moving/copying the file after first picking, makes it safer and reliable to access any allowed file on any Android device.
Aadarsh Patel
  • 167
  • 4
  • 14
0

I have an example for the file_picker package:

  var filePath = '';
  var fileExtension = '';
  var fileName = '';

  void buttonOnTap() async {
    try {
      filePath = await FilePicker.getFilePath(type: FileType.IMAGE);

      if (filePath != null) {
        fileName = filePath.split('/').last;
        fileExtension = fileName.split('.').last;
      } else {
        filePath = '';
        fileName = '';
        fileExtension = '';
      }
    } catch (e) {}

    if (filePath != '') {
      Image.file(File(filePath), fit: BoxFit.cover);
    }
  }
Nour Shobier
  • 539
  • 3
  • 6
  • 1
    I just tried file_picker but it automatically cached the image as well and "filePath" contains the path to the cached image, so this did not solve my problem – Milky Aug 10 '20 at 17:30
  • 2
    I got the same issue. Anyone know how to get the original path or specifically the original file extension? – fractal Jun 14 '21 at 13:12