如何解决如何在 Flutter 中显示 epub 文件的封面图片
dart-epub 插件提到了如何获取封面图片的示例:
// Book's cover image (null if there is no cover)
Image coverImage = epubBook.CoverImage;
我怎样才能显示这个?我一直收到错误
The following _TypeError was thrown building:
type 'Image' is not a subtype of type 'ImageProvider<Object>'
解决方法
更新:我想通了
导入图片和epub包为
import 'package:image/image.dart' as image;
import 'package:epub/epub.dart' as epub;
打开epub文件
// Change the location to wherever your epub file is located
var targetFile = new File('location/epubFile.epub');
List<int> bytes = await targetFile.readAsBytes();
epub.EpubBook epubBook = await epub.EpubReader.readBook(bytes);
将封面图片保存到某个位置(最好与 epub 文件相同的文件夹,以便一起删除)
// Save the Cover Image to some location
if (await File('location/epubFileCoverImage.png').exists()) {
print("File exists");
} else {
try {
File('location/epubFileCoverImage.png')
.writeAsBytesSync(image.encodePng(epubBook.CoverImage));
} catch (e) {
print("Error Saving Cover Image");
print(e);
coverError = true;
}
}
使用异常处理,因为加载某些 epub 文件的封面图像可能会出错 最后将其显示在 Widget 树中:
coverError ? Image.asset("assets/Error.png")
: Image.file(File('location/epubFileCoverImage.png')),
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。