Skip to content

[Bug report] Android平台 PNG 图片压缩,图片反而变大 #355

@chiclaim

Description

@chiclaim

Version

2.4.0

Platforms

Android

Device Model

VIVO X80

flutter info

flutter doctor -v                                          
[✓] Flutter (Channel stable, 3.27.4, on macOS 15.2 24C101 darwin-arm64, locale zh-Hans-CN)
    • Flutter version 3.27.4 on channel stable at /Users/yzq/dev/sdk/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision d8a9f9a52e (1 year, 2 months ago), 2025-01-31 16:07:18 -0500
    • Engine revision 82bd5b7209
    • Dart version 3.6.2
    • DevTools version 2.40.3
    • Pub download mirror https://pub-web.flutter-io.cn/
    • Flutter download mirror https://storage.flutter-io.cn

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at /Users/yzq/Library/Android/sdk
    • Platform android-35, build-tools 35.0.0
    • ANDROID_HOME = /Users/yzq/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16C5032a
    • CocoaPods version 1.16.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)

[✓] IntelliJ IDEA Ultimate Edition (version 2023.1.7)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.107.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.130.0

[✓] Connected device (5 available)
    • V2183A (mobile)                 • 10AC6C01GJ001LD                      • android-arm64  • Android 15 (API 35)
    • iPhone 16 Plus (mobile)         • DEB22F23-39D2-4394-B38A-413D54865FA3 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-18-2 (simulator)
    • macOS (desktop)                 • macos                                • darwin-arm64   • macOS 15.2 24C101 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad                • darwin         • macOS 15.2 24C101 darwin-arm64
    • Chrome (web)                    • chrome                               • web-javascript • Google Chrome 146.0.7680.80

[✓] Network resources
    • All expected network resources are available.

How to reproduce?

PNG 压缩前是 0.16995M
PNG压缩后是 1.231278M

PNG 图片不支持压缩,能否判断下,原图上传呢,至少不能让其变大了,感谢!

Logs

Example code (optional)

static Future<String> compressImage(String imagePath,
      {int minWidth = 1080}) async {
    final index = imagePath.lastIndexOf(".");
    final ext = imagePath.substring(index, imagePath.length).toLowerCase();
    final prefix = imagePath.substring(0, index);
    final targetPath = "${prefix}_compressed$ext";
    // 一定要判断 format,FlutterImageCompress.compressAndGetFile 默认是 CompressFormat.jpeg,如果上传的是 png 图片,就会报错
    CompressFormat format;
    if (ext == ".jpg" || ext == ".jpeg") {
      format = CompressFormat.jpeg;
    } else if (ext == ".png") {
      format = CompressFormat.png;
    } else if (ext == ".heic") {
      format = CompressFormat.heic;
    } else if (ext == ".webp") {
      format = CompressFormat.webp;
    } else {
      format = CompressFormat.jpeg;
    }
    var result = await FlutterImageCompress.compressAndGetFile(
      imagePath,
      targetPath,
      minWidth: minWidth,
      quality: 90,
      format: format,
    );
    if (isDebug) {
      printDebug("--------压缩前:${await File(imagePath).length() / 1024 / 1024}");
      printDebug("--------压缩后:${await result!.length() / 1024 / 1024}");
    }
    return targetPath;
  }
}


输出:
I/flutter (24898): --------压缩前:0.16995906829833984
I/flutter (24898): --------压缩后:1.231278419494629

Contact

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions