Android RangeError:颤振图像压缩中的问题

Android RangeError:颤振图像压缩中的问题,android,ios,flutter,compression,flutter-dependencies,Android,Ios,Flutter,Compression,Flutter Dependencies,我正在使用颤振插件 下面是代码 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 l

我正在使用颤振插件

下面是代码

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

为什么会发生这种情况?

我认为问题与字符串有关。您需要确保文件名以.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或.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)}";

 

好吧,但这不是绝对的答案。用户应该能够选择任何他想要的图像,不仅仅是jpgOkay,但这不是明确的答案。用户应该能够选择任何他想要的图像,而不仅仅是jpg