2012-06-07 65 views
3

我想讀取所有toc的所有內容。
你能告訴我如何閱讀內容表(toc)中的所有章節。
請發表修改後的代碼,以便我們可以閱讀所有內容或張貼一些內容以便閱讀。如何閱讀Android中的Epub文件的所有內容?

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     AssetManager assetsmanger=getAssets(); 

     try {         
      // find InputStream for book     
       InputStream epubInputStream=assetsmanger.open("books/INCOME-TAX-ACT-1961.epub"); 

       // Load Book from inputStream 
       Book book = (new EpubReader()).readEpub(epubInputStream); 

      // Log the book's authors       
       Log.i("epublib", "author(s): " +book.getMetadata().getAuthors()); 

       // Log the book's title 
      Log.i("epublib", "title: " + book.getTitle()); 

     String data=new String (book.getContents().get(3).getData()); 
     String k=data; 
       web1=(WebView)findViewById(R.id.webView1);  
//    txt1=(TextView) findViewById(R.id.textView1); 
//    txt1.setText(k); 
      web1.loadData(k," text/html", "utf8"); 

     Bitmap coverImage=BitmapFactory.decodeStream(book.getCoverImage().getInputStream()); 

     Log.i("epublib" , "Coverimage is " + coverImage.getWidth() + " by " 
        + coverImage.getHeight() + " pixels"); 
     // Log the tale of contents 
     logTableOfContents(book.getTableOfContents().getTocReferences(), 0); 

    //  list=(ExpandableListView)findViewById(R.id.expandableListView1); 


//  img1=(ImageView)findViewById(R.id.imageView1);  
//   img1.setImageBitmap(coverImage); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       Log.e("epublib", e.getMessage()); 
      } 
      catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

    private void logTableOfContents(List<TOCReference> tocReferences, int depth) { 
     // TODO Auto-generated method stub 

     if(tocReferences== null){ 
      return; 
     } 
     for(TOCReference tocReference:tocReferences){ 
      StringBuilder tocstring=new StringBuilder(); 
      for(int i=0;i<depth;i++) 
      { 
       tocstring.append("\t"); 

      } 
      HashMap<String, String> map = new HashMap<String, String>(); 


     String k= tocstring.append(tocReference.getTitle()).toString(); 
     map.put("TOC",k); 
     ArrayList<HashMap<String, String>> list1 = new ArrayList<HashMap<String, String>>(); 
     list1.add(map); 
     String t=k; 
      Log.i("epublib", tocstring.toString()); 
      logTableOfContents(tocReference.getChildren(), depth + 1); 

     }   
    }          
    } 
+0

什麼輸出所需的庫和sampleepubfile.epub你跟這個代碼的故事嗎? – GAMA

+0

可能的重複[如何閱讀所有內容根據智能章在Android](http://stackoverflow.com/questions/10895224/how-read-all-content-according-to-chapter-wise-in-android) – Barak

+0

我想一個接一個的閱讀所有的章節。 – user1388681

回答

5

使用此代碼,只要你有你的assets ...

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.List; 

import nl.siegmann.epublib.domain.Book; 
import nl.siegmann.epublib.domain.TOCReference; 
import nl.siegmann.epublib.epub.EpubReader; 
import android.app.Activity; 
import android.content.res.AssetManager; 
import android.os.Bundle; 
import android.text.Html; 
import android.util.Log; 
import android.webkit.WebView; 
public class EPubDemo extends Activity { 
    WebView webview; 
    String line, line1 = "", finalstr = ""; 
    int i = 0; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     webview = (WebView) findViewById(R.id.webview); 
     AssetManager assetManager = getAssets(); 
     try { 
      // find InputStream for book 
      InputStream epubInputStream = assetManager 
        .open("sampleepubfile.epub"); 

      // Load Book from inputStream 
      Book book = (new EpubReader()).readEpub(epubInputStream); 

      // Log the book's authors 
      Log.i("author", " : " + book.getMetadata().getAuthors()); 

      // Log the book's title 
      Log.i("title", " : " + book.getTitle()); 

      /* Log the book's coverimage property */ 
      // Bitmap coverImage = 
      // BitmapFactory.decodeStream(book.getCoverImage() 
      // .getInputStream()); 
      // Log.i("epublib", "Coverimage is " + coverImage.getWidth() + 
      // " by " 
      // + coverImage.getHeight() + " pixels"); 

      // Log the tale of contents 
      logTableOfContents(book.getTableOfContents().getTocReferences(), 0); 
     } catch (IOException e) { 
      Log.e("epublib exception", e.getMessage()); 
     } 

     String javascrips = ""; 
     try { 
      // InputStream input = getResources().openRawResource(R.raw.lights); 
      InputStream input = this.getAssets().open(
        "poe-fall-of-the-house-of-usher.epub"); 

      int size; 
      size = input.available(); 
      byte[] buffer = new byte[size]; 
      input.read(buffer); 
      input.close(); 
      // byte buffer into a string 
      javascrips = new String(buffer); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     // String html = readFile(is); 

     webview.loadDataWithBaseURL("file:///android_asset/", javascrips, 
       "application/epub+zip", "UTF-8", null); 
    } 

    @SuppressWarnings("unused") 
    private void logTableOfContents(List<TOCReference> tocReferences, int depth) { 
     if (tocReferences == null) { 
      return; 
     } 

     for (TOCReference tocReference : tocReferences) { 
      StringBuilder tocString = new StringBuilder(); 
      for (int i = 0; i < depth; i++) { 
      tocString.append("\t"); 
      } 
      tocString.append(tocReference.getTitle()); 
      Log.i("TOC", tocString.toString()); 

      try { 
       InputStream is = tocReference.getResource().getInputStream(); 
       BufferedReader r = new BufferedReader(new InputStreamReader(is)); 

       while ((line = r.readLine()) != null) { 
        // line1 = Html.fromHtml(line).toString(); 
        Log.v("line" + i, Html.fromHtml(line).toString()); 
        // line1 = (tocString.append(Html.fromHtml(line).toString()+ 
        // "\n")).toString(); 
        line1 = line1.concat(Html.fromHtml(line).toString()); 
       } 
       finalstr = finalstr.concat("\n").concat(line1); 
       // Log.v("Content " + i, finalstr); 
       i++; 
      } catch (IOException e) { 

      } 

      logTableOfContents(tocReference.getChildren(), depth + 1); 
     } 
     webview.loadDataWithBaseURL("", finalstr, "text/html", "UTF-8", ""); 
    } 
} 
+1

我有libaries,能夠讀取靜態章節從字符串data = new String(book.getContents()。get(3).getData());但我想閱讀所有內容章節明智,請告訴我如何做到這一點 – user1388681

+0

Thanx現在我能讀取它 – user1388681

+0

我想打開在線EPUB文件,所以請建議我該怎麼做? –