dart - Flutter:如何使用 Image 插件调整图像大小
问题描述
我正在尝试使用颤振插件“图像” https://pub.dartlang.org/packages/image调整图像大小
我按照说明使用的方法
File resizeMyImage(File resizeThisFile) {
// decodeImage will identify the format of the image and use the appropriate
// decoder.
File myCompressedFile;
Image image = decodeImage(resizeThisFile.readAsBytesSync());
// Resize the image to a 120x? thumbnail (maintaining the aspect ratio).
Image thumbnail = copyResize(image, 120);
// Save the thumbnail as a PNG.
myCompressedFile = new Io.File('thumbnail.png')..writeAsBytesSync(encodePng(thumbnail));
return myCompressedFile;
}
我得到的错误
E/flutter (22897): FileSystemException: Cannot open file, path = 'thumbnail.png' (OS Error: Read-only file system, errno = 30)
非常感谢任何帮助 - 谢谢。
工作调整大小类看起来像这样。希望它可以帮助某人。
import 'dart:async';
import 'dart:io' as Io;
import 'dart:io';
import 'package:image/image.dart';
import 'package:path_provider/path_provider.dart';
class ResizeImage {
String tempPath;
Future main() async {
}
Future<Io.File> resizeMyImage(File resizeThisFile) async {
Directory tempDir = await getTemporaryDirectory();
tempPath = tempDir.path;
// decodeImage will identify the format of the image and use the appropriate
// decoder.
File myCompressedFile;
Image image = decodeImage(resizeThisFile.readAsBytesSync());
// Resize the image to a 120x? thumbnail (maintaining the aspect ratio).
Image thumbnail = copyResize(image, 120);
// Save the thumbnail as a PNG.
print('resizeMyImage............tempPath: '+tempPath);
myCompressedFile = new Io.File(tempPath+'thumbnail.png')..writeAsBytesSync(encodePng(thumbnail));
return myCompressedFile;
}
}
解决方案
您不能在当前目录中创建文件,因为错误消息指出它是只读的。
https://pub.dartlang.org/packages/path_provider允许您获取有效目录的路径
Directory tempDir = await getTemporaryDirectory(); String tempPath = tempDir.path; Directory appDocDir = await getApplicationDocumentsDirectory(); String appDocPath = appDocDir.path;
推荐阅读
- listview - 小部件进入列表视图或颤动中的数据
- reactjs - 包括 id 的 ReactJs 路由路径返回不正确的组件
- c++ - 如何防止 CMAKE 在构建时为共享库创建符号链接(不在安装时)
- github - 仅在阻止拉取请求时手动运行 GitHub Actions
- sql - 在 MS Access 中查询分组数据
- gitlab - 更改 gitlab markdown 美人鱼图大小
- javascript - React Native“TypeError:Object(...)不是函数”反应导航堆栈错误
- javascript - 如何在组标题中添加可点击按钮?
- python - 代码正在执行应该在初始语句中被拒绝的过程,我不知道为什么
- javascript - 使用javascript在html中显示来自Firebase的嵌套数据?