android epublib 访问 css,images,urls 内部文件

标签 android epub epublib

我想在我的应用程序中使用 epub 图书。 我找到了这个链接 http://www.siegmann.nl/epublib/android打开我的 epub 书。

我了解如何显示 epub 书中的文本,

但我对图像、CSS 文件和链接有疑问。它们的 URL 指向 Assets 文件夹,例如:

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

并且在 Assets 文件夹中没有这样的 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:

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

最佳答案

无需解压 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
     }

替换webview中的html,将“/images/”改为“chosenfile/tempfolder”指向临时文件:

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

关于android epublib 访问 css,images,urls 内部文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29742577/

相关文章:

android - 如何在 Android 应用程序中自定义顶部状态栏?

javascript - 多行问卷可编辑

html - 正则表达式:查找 HTML 标记之间的小写字母组

java - 为什么 epublib for android web view 加载数据 url 显示 [B@41408d8?

Android EPUBLIB 读取/加载内容

带有 actionbaractivity 的 Android listactivity

android - 从对象列表中获取特定的object.value

android - UnknownPluginException 使用 Google Play 服务和插件 DSL

iphone - 在 epub 阅读器中通过 UIwebview 中的 xhtml 文件显示内容时出现问题