I am creating a book reader using siegmann epub library. I am not able to display anything on the screen but in the logcat. What type of layout to use for display?
Here is my main.java
public class MainActivity extends Activity {
private WebView webview;
private String line, line1="", finalstr="";
int i = 0;
private String fullBook;
private Book book;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView) findViewById(R.id.webview);
AssetManager assetManager = getAssets();
try{
//find input Stream for book
InputStream epubInputStream = assetManager.open("oliver_twist.epub");
//Load book from input stream
book = (new EpubReader()).readEpub(epubInputStream);
Log.i("epublib", "title: "+book.getTitle());
//Log the book's cover image property
//Bitmap coverImage = BitmapFactory.decodeStream(book.getCoverImage().getInputStream());
//Log.i("epublib", "CoverImage is" + coverImage.getWidth()+" by "+coverImage.getHeight()+" pixels");
//Log the tables of contents
logTableOfContents(book.getTableOfContents().getTocReferences(),0);
fullBook = getEntireBook();
}
catch(IOException e){
Log.e("epublib", 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);
//webview.setwe
}
private void logTableOfContents(List<TOCReference> tocReferences, int depth){
if(tocReferences == null){
System.out.println("---->>>"+tocReferences);
return;
}
for(TOCReference tocReference:tocReferences){
StringBuilder tocString = new StringBuilder();
for(int i=0;i<depth;i++){
tocString.append(" ");
}
tocString.append(tocReference.getTitle());
Log.i("epulib", 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()+
// "
")).toString();
line1 = line1.concat(Html.fromHtml(line).toString());
}
finalstr = finalstr.concat("
").concat(line1);
Log.i("Content " + i, finalstr);
i++;
} catch (IOException e) {
}
logTableOfContents(tocReference.getChildren(), depth+1);
}
webview.loadDataWithBaseURL("", finalstr, "text/html", "UTF-8", "");
}
public String getEntireBook(){
String line, linez = null;
Spine spine = book.getSpine();
Resource res;
List<SpineReference> spineList = spine.getSpineReferences() ;
int count = spineList.size();
int start = 0;
StringBuilder string = new StringBuilder();
for (int i = start; count > i; i = i +1) {
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 + "
").toString();
}
} catch (IOException e) {e.printStackTrace();}
} catch (IOException e) {
e.printStackTrace();
}
}
return linez;
}
}
In main.xml, I have just created a webview. What otehr elemnets I can add? Is it a text view on which book is displayed?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…