【问题标题】:How to display Cover Image of an epub file in Flutter如何在 Flutter 中显示 epub 文件的封面图片
【发布时间】:2021-06-17 20:39:32
【问题描述】:
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>'
【问题讨论】:
标签:
flutter
epub
flutter-plugin
【解决方案1】:
更新:我想通了
导入图片和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')),