2011-11-24 48 views
5

這是我試圖執行任務,如果任何人都可以幫助它將不勝感激。所以在這段代碼中它只會顯示封面。我讀http://www.siegmann.nl/static/epublib/apidocs/,你可以使用getSpine()來獲取所有內容,但它只在我的封面上顯示了一件事情,即封面。如何顯示所有頁面和所有章節使用nl.siegmann.epublib

webView = (WebView)findViewById(R.id.webView); 
webView.getSettings().setJavaScriptEnabled(true); 
AssetManager am = getAssets(); 
try { 
    InputStream epubInputStream = am.open(bookName); 
    book = (new EpubReader()).readEpub(epubInputStream); 
} catch (IOException e) { 
    Log.e("epublib", e.getMessage()); 
} 

Spine spine = book.getSpine(); 
for (SpineReference bookSection : spine.getSpineReferences()) { 
    Resource res = bookSection.getResource(); 

    try { 
     InputStream is = res.getInputStream(); 
     StringBuffer string = new StringBuffer(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 

     try { 
      while ((line = reader.readLine()) != null) { 
       linez = string.append(line + "\n").toString(); 
      } 
     } catch (IOException e) {e.printStackTrace();} 

     //do something with stream 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 
webView.loadData(linez, "text/html", "utf-8"); 
+0

我解決了這個問題,因爲我的回答少於100個重點,所以當我回家的時候,我的回答很糟糕。 – wesdfgfgd

+0

你好,你能發表答覆嗎?謝謝 – TilalHusain

回答

3

所以我想通過http://www.siegmann.nl/static/epublib/apidocs/上的spine發現它仍然可以分段工作。所以我試圖通過識別計數來找出有多少部分。然後把這些數字放在Resource res = spine.getResource(i);。如果你願意的話Resource res = spine.getResource(2);它會顯示2的脊椎,除非有人弄亂了epub的格式,否則應該是第2章。

Spine spine = book.getSpine(); 
List<SpineReference> spineList = spine.getSpineReferences() ; 
int count = spineList.size(); 
tv.setText(Integer.toString(count)); 
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 { 
      while ((line = reader.readLine()) != null) { 
       linez = string.append(line + "\n").toString(); 
      } 

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

     //do something with stream 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 
webView.loadData(linez, "text/html", "utf-8"); 
+0

'Resource res','InputStream is','BufferedReader reader'應該在循環之外聲明...另外,除非確實需要,否則請避免使用大量的try-catch。 – GAMA

+0

如何識別字符串中的頁面? –

相關問題