Flutter:如何使用Image插件调整图像大小

4

我正在尝试使用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;
  }
}
1个回答

0

无法在当前目录中创建文件,因为正如错误消息所述,它是只读的。

https://pub.dartlang.org/packages/path_provider 允许您获取有效目录的路径

Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;

Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;

谢谢您的帮助。我也是这么想的,查看了文档以查看他们是否使用插件来查找路径,但他们没有,所以我认为我可能漏掉了什么。 - AhabLives
你所说的“they”是指谁?你指的是什么? - Günter Zöchbauer
来自 pub.dartlang 链接的插件的创建者。他们在插件包中提供了一个示例。再次感谢您的帮助。我想现在它已经可以工作了。可以安全地假设 pub.dartlang.org 的软件包不是专门为 flutter 设计的吗? - AhabLives
不,只有在“Flutter”是唯一的标签时才能使用。其他标签可以用于Dart命令行或浏览器应用程序。 - Günter Zöchbauer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接