【问题标题】:Flutter: How to resize an image with Image pluginFlutter:如何使用 Image 插件调整图像大小
【发布时间】:2018-06-15 02:22:08
【问题描述】:

我正在尝试使用颤振插件“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;
  }
}

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    您不能在当前目录中创建文件,因为错误消息指出它是只读的。

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

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

    【讨论】:

    • 感谢您的帮助。我也有同样的想法,并查看了文档以查看他们是否使用插件来查找路径,而他们不是,所以我认为我错过了一些东西。
    • “他们”是指谁?你指的是什么?
    • 来自 pub.dartlang 链接的插件的创建者。他们在插件包中有一个示例。再次感谢您的帮助。我想我现在可以工作了。是否可以假设 pub.dartlang.org 包不是专门用于颤振的?
    • 不,仅当Flutter 是唯一标签时。其他的可以在 Dart 命令行或浏览器应用中使用。
    猜你喜欢
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2022-11-23
    • 1970-01-01
    • 2021-04-20
    • 2014-05-19
    相关资源
    最近更新 更多