2011-11-24 52 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

如何识别字符串中的页面? –

相关问题