来自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
没有长度,这意味着它不存在,我们可以从输出错误中确认。
可能出现多种问题:
1.在iPad上,directoryPath
不一致,不应使用。
1.在iPad上,压缩算法无法写入内存。
- 注:
widget.directoryPath
的值为(await getApplicationDocumentsDirectory()).path
*
1条答案
按热度按时间tag5nh1u1#
我也遇到过类似的问题。原因是因为文件路径的名称超过了限制,就像很少有人在评论中分享的那样,因此当你试图访问文件时,它不会成功保存文件,它会给出错误,即没有这样的文件/目录。请确保您的文件路径在限制范围内,这将解决问题。