Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying images using EPUBLIB

I am using epublib to read a .epub file in a WebView.

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

The DownloadResource works fine. The resources are fetched. But the WebView is not displaying the image. The images are in the same directory as the epub file. The WebView gives me this:

like image 617
Aravind Srivatsan Avatar asked Oct 19 '12 00:10

Aravind Srivatsan


1 Answers

It's been while for this problem but if anyone encountered it, I encountered the same problem and the solution was pretty simple which got ignored.

wv.loadDataWithBaseURL(abspath.substring(0, pos)+"/", sb.toString(), "text/html", "utf-8", null);

This line specifies the resources home / webpage origin url with abspath.substring(0, pos)+"/" this part of code.

But we did not mention it the protocol, i.e. http, ftp, file (local) so the fix was

wv.loadDataWithBaseURL("file://"+abspath.substring(0, pos)+"/", sb.toString(), "text/html", "utf-8", null);

and it worked like a charm :)

like image 84
Ankit Pise Avatar answered Oct 13 '22 01:10

Ankit Pise