2012-10-19 4 views
5

epublib을 사용하여 WebView에서 .epub 파일을 읽습니다.EPUBLIB를 사용하여 이미지 표시

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 파일과 동일한 디렉토리에 있습니다. 당신은 루프 자원이 같은 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; 
    } 

을 얻을 수 후

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

을이 방법 :

+0

처럼 일 텍스트 (.xhtml 파일)는 어떻게 생겼습니까? – Freney

+1

이 문제를 해결합니까? – Villan

답변

0

먼저이 같은 모든 자원을 얻을해야합니다 웹보기이 나에게 준다 그 것을 사용하는 것

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

희망이 도움이된다. 너.

1

이 문제가 발생하는 동안 누군가 만났을 때 동일한 문제가 발생했으며 해결 방법은 매우 단순합니다.

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

이었고, 그것은 매력 :) 그 소스를 무엇

+0

sample_book이라는 이름을 가진 내 전자 북은 assets/epub 폴더 안에 있습니다. 어떤 절대 URL을 사용해야합니까? 현재이 애셋을 열 수 없습니다. 'file : /// android_asset/epub/sample_book/docimages/cover.jpg' –

+1

경로가 올바르지 않은 것 같습니다./of 파일 시스템에서 시작해야합니다. 예 :/data/data/YOUR_PACKAGE_NAME –