You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

flutter_image_compress在iPad上写入targetPath始终失败

FlutterImageCompress.compressAndGetFile在iPad上无法写入目标路径问题

flutter_image_compress库中的FlutterImageCompress.compressAndGetFile方法在所有iPad设备上都无法将输出内容写入targetPath,但完全相同的代码在iPhone以及所有Android手机和平板上均可正常运行。

问题代码

Future<File?> _compressFile(File file) async {
  final String path =
      '${widget.directoryPath}/users/${User().id}/images/tmp.jpeg';
  final File? result = await FlutterImageCompress.compressAndGetFile(
      file.absolute.path, path,
      quality: 50);

  print(result);

  try {
    await result!
        .length(); // from the docs: The returned file may be null. In addition, please decide for yourself whether the file exists.
    print(
        'Compress file from ${(await file.length()) / 1e6} Mb to ${(await result.length()) / 1e6} Mb');
  } catch (e) {
    print('File original size ${(await file.length()) / 1e6} Mb');
    print('Error while compressing file: $e');
    return null;
  }

  return result;
}

iPad上的输出日志

flutter: File: '/Users/adrkacz/Library/Developer/CoreSimulator/Devices/C2E0A11D-6BC4-47FF-BCAE-A15709073B9B/data/Containers/Data/Application/587B3363-828F-489F-8490-B2099F02D985/Documents/users/a38ededd-5fd5-4639-a303-40c4289cace8/images/tmp.jpeg'
flutter: File original size 1.74672 Mb
flutter: Error while compressing file: PathNotFoundException: Cannot retrieve length of file, path = '/Users/adrkacz/Library/Developer/CoreSimulator/Devices/C2E0A11D-6BC4-47FF-BCAE-A15709073B9B/data/Containers/Data/Application/587B3363-828F-489F-8490-B2099F02D985/Documents/users/a38ededd-5fd5-4639-a303-40c4289cace8/images/tmp.jpeg' (OS Error: No such file or directory, errno = 2)

观察到的现象

  • FlutterImageCompress.compressAndGetFile未返回null,而是返回了预期路径下的File对象
  • 原文件file非空,程序可读取其大小(1.74672 Mb)
  • result文件不存在,无法获取其大小,错误输出已确认

可能的问题原因

  • iPad上的directoryPath存在不一致问题,不应直接使用
  • iPad上的压缩算法无法写入指定路径

注:widget.directoryPath的值为(await getApplicationDocumentsDirectory()).path的返回内容

内容的提问来源于stack exchange,提问作者Adrien Kaczmarek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 19:47:26