2015-04-20 42 views
1

我想在我的应用程序中使用epub书。 我发现这个链接http://www.siegmann.nl/epublib/android打开我的epub书。android epublib访问css,图像,url内部文件

我知道如何显示来自EPUB书的文本,

,但我有一个图片,CSS文件和链接的一个问题。他们的网址是资产文件夹,例如:

文件:///android_asset/index_split_000.html#back_note_1 文件:///android_asset/images/image-1.jpeg

而在资产文件夹没有像这样的HTML页面,没有图像文件夹,只有epub压缩文件。

如何使用所有内部文件?

我的代码:

WebView webView=(WebView) findViewById(webView); 
List<Chapter> chapters=new ArrayList<Chapter>(); 
AssetManager assetManager = getAssets(); 
    try{ 
     //find input Stream for book 
     InputStream epubInputStream = assetManager.open("xxxxx.epub"); 
     //Load book from input stream 
     book = (new EpubReader()).readEpub(epubInputStream); 

     //Log the book's cover image property 
     Bitmap coverImage =  BitmapFactory.decodeStream(book.getCoverImage().getInputStream()); 
     //Log.i("epublib", "CoverImage is" + coverImage.getWidth()+" by "+coverImage.getHeight()+" pixels"); 

     //Log the tables of contents 
     logTableOfContents(book.getTableOfContents().getTocReferences(),0); 


    } 
    catch(IOException e){ 
     Log.e("epublib", e.getMessage()); 
    } 




private void logTableOfContents(List<TOCReference> tocReferences, int depth) { 

    if (tocReferences == null) { 

     return; 

    } 

    for (TOCReference tocReference : tocReferences) { 

     StringBuilder tocString = new StringBuilder(); 



     try { 
      Chapter chapter=new Chapter(); 

      String s=new String(tocReference.getResource().getData()); 
      chapter.setTitle(tocReference.getTitle()); 
      chapter.setContent(s); 
      chapters.add(chapter); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

     logTableOfContents(tocReference.getChildren(), depth + 1); 

    } 

    } 

章是我自己的类:

public class Chapter { 

String title; 
String content; 

public Chapter(String titlt,String content) { 
    this.title=titlt; 
    this.content=content; 
} 

public Chapter() { 
} 

public String getTitle() { 
    return title; 
} 
public String getContent() { 
    return content; 
} 
public void setTitle(String title) { 
    this.title = title; 
} 
public void setContent(String content) { 
    this.content = content; 
} 

} 

将数据加载到网页视图:

webView.loadDataWithBaseURL("file:///android_asset/",chapters.get(index).getContent(), "text/html", "utf-8", null); 

回答

2

一个简单的方法包括在解压缩EPUB文件到一个临时目录中,然后从那里加载XHTML文件。通过这种方式,每个XHTML文件中存在的图像文件,CSS文件等的相关链接将由WebView自动解析。

这种技术的一个例子是在这里:https://github.com/pettarin/epub3reader

当然,你可避免解压缩整个EPUB文件,而只是“读”一个XHTML文件到内存(RAM),但如果你这样做,你必须修补要注入WebView的XHTML源代码,和/或手动管理图像/ CSS /等的提取。从当前XHTML引用的文件。如果你的目标是“简单的”EPUB文件,那么“只需在tmp目录下解压缩”的方法就容易多了。

2

不解压缩epub库。

所有你需要做的就是将位图图像,你可以很容易使用epublib做。然后,我将它们保存在一个临时文件夹中,并在html中使用string.replace,以确保html现在指向新的临时目录,而不是webview将不知道“/ images”的位置。

通过所有的位图获取所有的bipmaps作为资源

MediaType[] bitmapTypes = { MediatypeService.PNG, MediatypeService.GIF, MediatypeService.JPG }; 
List<Resource> bitmapResources = book.getResources().getResourcesByMediaTypes(bitmapTypes); 

环路作为资源:

for(Resource r : bitmapResources) { 
      Bitmap bm = BitmapFactory.decodeByteArray(r.getData(), 0, r.getData().length); 
     // store in a public folder on android 
    } 

在网页视图替换HTML中, “/图片/” 到“chosenfile/tempfolder “指向临时文件:

String imagePath = "file://" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/temp_images" + File.separator; 
    text = text.replaceAll("images/", imagePath); 
    webView.loadDataWithBaseURL("", text, "text/html", "UTF-8", ""); 
+0

不能多谢你。先生,你是救命的人。 –