【问题标题】:RangeError: Issue in flutter image compressingRangeError:颤动图像压缩问题
【发布时间】:2021-01-31 08:16:03
【问题描述】:

我正在使用flutter插件flutter_image_compress

下面是代码

File compressImage(File file)  {
    // Get file path
    // eg:- "Volume/VM/abcd.jpeg"
    final filePath = file.absolute.path;
    
    // Create output file path
    // eg:- "Volume/VM/abcd_out.jpeg"
    final lastIndex = filePath.lastIndexOf(new RegExp(r'.jp'));
    final splitted = filePath.substring(0, (lastIndex));
    final outPath = "${splitted}_out${filePath.substring(lastIndex)}";

    File compressedFile;

    FlutterImageCompress.compressAndGetFile(
          filePath, 
          outPath,
          minWidth: 1000,
          minHeight: 1000,
          quality: 70).then((value) => compressedFile = value);

    return compressedFile;
}

这会返回以下错误

RangeError: Value not in range: -1

为什么会这样?

【问题讨论】:

    标签: android ios flutter compression flutter-dependencies


    【解决方案1】:

    我认为问题与字符串有关。您需要确保您的文件名以 .jpg 或 .jpeg 结尾,而不是以 .png 或类似名称结尾。

    final filePath = file.absolute.path;
    
    // Create output file path
    // eg:- "Volume/VM/abcd_out.jpeg"
    final lastIndex = filePath.lastIndexOf(new RegExp(r'.jp'));
    final splitted = filePath.substring(0, (lastIndex));
    final outPath = "${splitted}_out${filePath.substring(lastIndex)}";
    
     
    

    【讨论】:

    • 好的,但这不是绝对的答案。用户应该能够选择他想要的任何图像,而不仅仅是 jpg
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 2020-02-29
    相关资源
    最近更新 更多