6

我正在使用 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 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());
     }
}

DownloadResource 工作正常。资源被获取。但是 WebView 不显示图像。这些图像与 epub 文件位于同一目录中。WebView 给了我这个:

4

2 回答 2

2

这个问题已经有一段时间了,但是如果有人遇到它,我遇到了同样的问题,解决方案非常简单,被忽略了。

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);

它就像一个魅力:)

于 2015-11-30T07:02:20.943 回答
1

首先你必须得到这样的所有资源:

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

之后,您可以循环资源以获取相同的 href 文件

private Bitmap getBitmapFromResources(List<Resource> resources, String imgHref)
    {
        byte[] data = "holder".getBytes();
        for(int ii = 0; ii < resources.size();ii++)
        {
            String z = resources.get(ii).getHref();
            if(z.equals(imgHref))
            {
                Log.i("livi", z);
                try {
                    data = resources.get(ii).getData();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            }
        }
        //
            Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
            return bm;
    }

这如何使用

Bitmap bm = getBitmapFromResources(resources, "cover.jpg");
            if (bm != null)
                ivTest.setImageBitmap(bm);

希望这对你有帮助。

于 2014-09-30T01:06:19.773 回答