如何在 Flutter 中获取使用图像选择器插件选择的图像的原始路径而不是复制到缓存?

Mil*_*lky 8 dart flutter flutter-image

我正在制作一个 Android 应用程序,并且在使用图像选择器插件时;

final pickedFile = await ImagePicker().getImage(source: ImageSource.gallery);
Run Code Online (Sandbox Code Playgroud)
File image = File(pickedFile.path);
Run Code Online (Sandbox Code Playgroud)

“图像”是应用程序缓存中原始图像的副本,但是,我想直接使用原始图像的路径将其保存在我的应用程序中,因为我不希望应用程序缓存大小增加。我看到不推荐使用的方法“pickImage”完成了这一点(不复制到缓存),但新的“getImage”似乎自动复制,并且“图像的路径”是缓存图像的路径。

如何在不缓存的情况下仅获取所选图像的原始路径?(我假设使用原始文件的路径仍然可以在应用程序中显示它FileImage(File(originalPath)),这是正确的假设吗?)

小智 0

我有一个file_picker包的示例:

  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);
    }
  }
Run Code Online (Sandbox Code Playgroud)

  • 我遇到了同样的问题。有人知道如何获取原始路径或特别是原始文件扩展名吗? (2认同)