2017-04-12 79 views
-3

我正在爲我的客戶端在android中創建epub書籍閱讀器應用程序。 我目前在我的應用程序中使用FolioReader,但它不顯示圖像。是否有任何其他替代方法在Android中顯示.epub文件。Android:解析.epub文件

在此先感謝!

+0

儘量EpubParser https://github.com/mertakdut/EpubParser – AMB

+0

@SpringBreaker它仍然不是DISPLA ying images – Ravi

+0

@Redman圖片顯示方式不正確 – Ravi

回答

1

您必須提取圖像並在webview中加載數據時保留對這些圖像的引用。試試這個代碼(添加EPUB lib和SLF4J庫)

 outPutFolder=getDir("example", Context.MODE_PRIVATE) + "/"+fileName +"/" ; 
      //your directory name 

    try { 

     epubInputStream=new FileInputStream(filePath); 

    } catch (IOException e1) { 

     e1.printStackTrace(); 
    } 


    try { 
     book = (new EpubReader()).readEpub(epubInputStream); 
    } catch (IOException e) { 

     e.printStackTrace(); 
    } 

    outPutFolder=getDir("example", Context.MODE_PRIVATE) + "/"+fileName +"/" ; 
    setOutPutFolder(outPutFolder); 

    DownloadResource(outPutFolder); 



    linezNew = ""; 
    Spine spine = book.getSpine(); 
    List<SpineReference> spineList = spine.getSpineReferences() ; 
    int count = spineList.size(); 

    StringBuilder string = new StringBuilder(); 
    for (int i = 0; count > i; i++) { 

     Resource res = spine.getResource(i); 

     try { 
      InputStream is = res.getInputStream(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
      try { 
       String line; 
       while ((line = reader.readLine()) != null) { 
        linezNew = string.append(line + "\n").toString(); 
       } 

      } catch (IOException e) {e.printStackTrace();} 


     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

    linezNew = linezNew.replace("../", ""); 

    finalDataToLoad="<head><style>img{max-width: 100%; width:50%; height: 50%;}</style></head> "+linezNew; 


    webView.loadDataWithBaseURL("file://" + outPutFolder, finalDataToLoad, "text/html", "utf-8", null); 

和DownloadResource功能

private void DownloadResource(String directory) { 
    try { 

     Resources rst = book.getResources(); 
     Collection<Resource> clrst = rst.getAll(); 
     Iterator<Resource> itr = clrst.iterator(); 

     while (itr.hasNext()) { 
      Resource rs = itr.next(); 

      if ((rs.getMediaType() == MediatypeService.JPG) 
        || (rs.getMediaType() == MediatypeService.PNG) 
        || (rs.getMediaType() == MediatypeService.GIF)) { 

       Log.d("Href", rs.getHref()); 

       File oppath1 = new File(directory, rs.getHref().replace("OEBPS/", "")); 

       oppath1.getParentFile().mkdirs(); 
       oppath1.createNewFile(); 

       System.out.println("Path : "+oppath1.getParentFile().getAbsolutePath()); 


       FileOutputStream fos1 = new FileOutputStream(oppath1); 
       fos1.write(rs.getData()); 
       fos1.close(); 

      } else if (rs.getMediaType() == MediatypeService.CSS) { 

       File oppath = new File(directory, rs.getHref()); 

       oppath.getParentFile().mkdirs(); 
       oppath.createNewFile(); 

       FileOutputStream fos = new FileOutputStream(oppath); 
       fos.write(rs.getData()); 
       fos.close(); 

      } 

     } 

    } catch (Exception e) { 

    } 
} 

這裏,filepath是路徑到您的EPUB文件,文件名是是字符串

您的EPUB文件的名稱。

outPutFolder,linezNew,finalDataToLoad也串