Android 使用EPUBLIB显示图像
我正在使用epublib在WebView中读取.epub文件Android 使用EPUBLIB显示图像,android,android-webview,epub,android-file,epublib,Android,Android Webview,Epub,Android File,Epublib,我正在使用epublib在WebView中读取.epub文件 WebView wv = (WebView) getView().findViewById(R.id.chaptercontent); try { String abspath = FILEPATH+file; File filePath = new File(abspath+".epub"); InputStream epubInputStream = new Buffe
WebView wv = (WebView) getView().findViewById(R.id.chaptercontent);
try {
String abspath = FILEPATH+file;
File filePath = new File(abspath+".epub");
InputStream epubInputStream = new BufferedInputStream(new FileInputStream(filePath));
book = (new EpubReader()).readEpub(epubInputStream);
int pos = abspath.lastIndexOf('/');
DownloadResource(abspath.substring(0, pos));
try {
for(int i = 1; i< book.getContents().size(); i++) {
InputStream is = book.getSpine().getSpineReferences().get(i).getResource().getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
Log.d("display line", line);
}
is.close();
wv.loadDataWithBaseURL(abspath.substring(0, pos)+"/", sb.toString(), "text/html", "utf-8", null);
}
}
catch(IOException e) {
Log.e("IOException", e.getMessage());
}
}
catch (IOException e) {
Log.e("epublib", e.getMessage());
}
private void DownloadResource(String directory) {
try {
nl.siegmann.epublib.domain.Resources rst = book.getResources();
Collection<Resource> clrst = rst.getAll();
Iterator<Resource> itr = clrst.iterator();
Log.d("Downlod path", directory);
while (itr.hasNext()) {
Resource rs = itr.next();
if ((rs.getMediaType() == MediatypeService.JPG) || (rs.getMediaType() == MediatypeService.PNG) || (rs.getMediaType() == MediatypeService.GIF) || rs.getMediaType() == MediatypeService.CSS) {
File oppath1 = new File(directory+File.separator+rs.getHref());
Log.d("Resource Name - ", rs.getHref());
oppath1.createNewFile();
Log.d("Oppath - ", oppath1.getAbsolutePath());
Log.d("File Checking - ", "Exists - "+oppath1.exists()+" & Write - "+oppath1.canWrite());
FileOutputStream fos1 = new FileOutputStream(oppath1);
fos1.write(rs.getData());
fos1.close();
}
}
}
catch (IOException e) {
Log.e("error", e.getMessage());
}
}
WebView wv=(WebView)getView().findViewById(R.id.chaptercontent);
试一试{
字符串abspath=FILEPATH+file;
文件路径=新文件(abspath+“.epub”);
InputStream epubInputStream=新的BufferedInputStream(新的FileInputStream(filePath));
book=(新的EpubReader()).readEpub(epubInputStream);
int pos=abspath.lastIndexOf('/');
下载资源(abspath.substring(0,pos));
试一试{
for(inti=1;i
下载资源工作正常。资源已被提取。但是WebView没有显示图像。这些图像与epub文件位于同一目录中。WebView为我提供了以下信息:
首先,您必须获得以下所有资源:
MediaType[] bitmapTypes = { MediatypeService.PNG,
MediatypeService.GIF, MediatypeService.JPG };
List<Resource> resources = book.getResources().getResourcesByMediaTypes(bitmapTypes);
希望这对您有所帮助。这个问题已经有一段时间了,但如果有人遇到了,我也遇到了同样的问题,解决方案非常简单,但被忽略了
wv.loadDataWithBaseURL(abspath.substring(0, pos)+"/", sb.toString(), "text/html", "utf-8", null);
此行使用abspath.substring(0,pos)+“/”
这部分代码指定资源主页/网页源url
但我们没有提到它的协议,即http,ftp,文件(本地),所以修复
wv.loadDataWithBaseURL("file://"+abspath.substring(0, pos)+"/", sb.toString(), "text/html", "utf-8", null);
它工作起来很有魅力:)该文本的源代码(即.xhtml文件)是什么样子的?我的名为sample_book的电子书位于assets/epub文件夹中。我必须使用哪个绝对URL?我当前遇到此错误
无法打开资产URL:file:///android_asset/epub/sample_book/docimages/cover.jpg
您的路径似乎不正确,它应该从/文件系统开始。e、 g./数据/数据/您的包名称
wv.loadDataWithBaseURL(abspath.substring(0, pos)+"/", sb.toString(), "text/html", "utf-8", null);
wv.loadDataWithBaseURL("file://"+abspath.substring(0, pos)+"/", sb.toString(), "text/html", "utf-8", null);