2012-10-19 13 views
5

Sto usando epublib per leggere un file .epub in una WebView.Visualizzazione di immagini tramite 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 funziona correttamente. Le risorse vengono recuperate. Ma la WebView non sta visualizzando l'immagine. Le immagini si trovano nella stessa directory del file epub. Il WebView mi dà questo:

+0

Che cosa significa la fonte per questo testo (il file .xhtml) assomiglia? – Freney

+1

Risolvi questo? – Villan

risposta

0

primo luogo è necessario ottenere tutte le risorse in questo modo:

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

dopo che si può ciclo le risorse per ottenere stesso file 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; 
    } 

e questo modo per utilizzarlo

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

spero che questo aiuto tu.

1

E 'stato per questo problema, ma se qualcuno lo ha incontrato, ho riscontrato lo stesso problema e la soluzione era piuttosto semplice che è stata ignorata.

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

Questa riga specifica delle risorse home/pagina web origine url con abspath.substring(0, pos)+"/" questa parte del codice.

Ma non abbiamo fatto menzione che il protocollo, vale a dire http, ftp, file di (locale) in modo che la correzione è stata

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

e ha funzionato come un fascino :)

+0

Il mio ebook con nome sample_book è all'interno della cartella assets/epub. Quale URL assoluto devo usare? Attualmente sto ricevendo questo errore 'Impossibile aprire l'URL della risorsa: file: /// android_asset/epub/sample_book/docimages/cover.jpg' –

+1

Il tuo percorso non sembra corretto, dovrebbe iniziare da/del filesystem. per esempio./Dati/data/YOUR_PACKAGE_NAME –

Problemi correlati