我试图用dart解码一些动画的webp文件,但是得到了“索引超出范围”的运行时错误,它应该小于N,而N是文件的大小(以字节为单位)。但是这个文件夹以外的图像工作得很好。
使用链接的 https://github.com/WhatsApp/stickers/tree/main/Android/app/src/main/assets/2动画网页文件
Dart代码:
import 'dart:io';
import 'package:image/image.dart';
void main() {
final image = 'asset/ok.webp';
File bytes = File(image);
List<int> list = bytes.readAsBytesSync();
// list.length = 85538
Animation anim = decodeWebPAnimation(list)!;
// runtime error raised from decodeWebPAnimation()
}控制台:
Exception has occurred.
IndexError (RangeError (index): Index out of range: index should be less than 5292: 5292)工具版本:
Flutter 2.10.2 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 097d3313d8 (6 days ago) • 2022-02-18 19:33:08 -0600
Engine • revision a83ed0e5e3
Tools • Dart 2.16.1 • DevTools 2.9.2PS:我已经检查了关于堆栈溢出的类似问题,但是这是基于颤振UI组件的,所以组件和答案引起的错误只与UI组件相关。
发布于 2022-02-25 15:14:14
import 'package:flutter/services.dart' show rootBundle;
import 'package:path_provider/path_provider.dart'
final image = await rootBundle.load('images/07_OK.webp');
Directory pa = await p.getTemporaryDirectory();
File file = File(pa.path+"/xyz.webp");
await file.writeAsBytes(image.buffer.asUint8List(image.offsetInBytes, image.lengthInBytes));
List<int> list = await file.readAsBytes();
Animation anim = decodeWebPAnimation(list)!;也许,这能解决你的问题
https://stackoverflow.com/questions/71265285
复制相似问题