我正在尝试使用
阅读并在android中显示epub的内容public class MainActivity extends Activity {
WebView webview;
String line, line1 = "", finalstr = "";
int i = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView) findViewById(R.id.tv);
AssetManager assetManager = getAssets();
try {
InputStream epubInputStream = assetManager
.open("life.epub");
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 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(
"life.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", "");
}
}
我的logcat显示一个警告并在logacat中显示为
02-04 16:33:54.319: W/System.err(32455): 499 [main] ERROR nl.siegmann.epublib.epub.PackageDocumentReader - Could not find table of contents resource. Tried resource with id '', toc, TOC and any NCX resource.
02-04 16:33:54.319: W/System.err(32455): 500 [main] ERROR nl.siegmann.epublib.epub.NCXDocument - Book does not contain a table of contents file
02-04 16:33:54.319: I/author(32455): : [Dylan, Jesse]
02-04 16:33:54.319: I/title(32455): : Thomas Cole - The Voyage of Life
02-04 16:33:54.329: D/dalvikvm(32455): GC_FOR_ALLOC freed 1038K, 19% free 7619K/9352K, paused 14ms, total 14ms
02-04 16:33:54.329: I/dalvikvm-heap(32455): Grow heap (frag case) to 8.541MB for 970907-byte allocation
02-04 16:33:54.349: D/dalvikvm(32455): GC_CONCURRENT freed <1K, 17% free 8567K/10304K, paused 2ms+2ms, total 16ms
02-04 16:33:54.349: D/dalvikvm(32455): WAIT_FOR_CONCURRENT_GC blocked 11ms
02-04 16:33:54.349: D/dalvikvm(32455): WAIT_FOR_CONCURRENT_GC blocked 14ms
02-04 16:33:54.369: D/dalvikvm(32455): GC_FOR_ALLOC freed 0K, 17% free 8567K/10304K, paused 15ms, total 15ms
02-04 16:33:54.369: I/dalvikvm-heap(32455): Grow heap (frag case) to 10.392MB for 1941798-byte allocation
02-04 16:33:54.369: D/dalvikvm(32455): WAIT_FOR_CONCURRENT_GC blocked 32ms
02-04 16:33:54.369: I/dalvikvm-heap(32455): Grow heap (frag case) to 10.392MB for 24-byte allocation
02-04 16:33:54.389: D/dalvikvm(32455): GC_FOR_ALLOC freed 0K, 15% free 10463K/12204K, paused 17ms, total 17ms
02-04 16:33:54.409: D/dalvikvm(32455): GC_CONCURRENT freed <1K, 15% free 10464K/12204K, paused 4ms+3ms, total 21ms
02-04 16:33:54.809: D/dalvikvm(32455): GC_FOR_ALLOC freed <1K, 15% free 10464K/12204K, paused 13ms, total 13ms
02-04 16:33:54.819: I/dalvikvm-heap(32455): Grow heap (frag case) to 12.118MB for 1807698-byte allocation
02-04 16:33:54.839: D/dalvikvm(32455): GC_FOR_ALLOC freed 1896K, 27% free 10333K/13972K, paused 28ms, total 28ms
02-04 16:33:54.869: D/dalvikvm(32455): GC_CONCURRENT freed 951K, 33% free 9395K/13972K, paused 3ms+2ms, total 22ms
02-04 16:33:54.889: D/libEGL(32455): loaded /system/lib/egl/libEGL_tegra.so
02-04 16:33:54.909: D/libEGL(32455): loaded /system/lib/egl/libGLESv1_CM_tegra.so
02-04 16:33:54.919: D/libEGL(32455): loaded /system/lib/egl/libGLESv2_tegra.so
02-04 16:33:54.939: D/OpenGLRenderer(32455): Enabling debug mode 0
02-04 16:33:55.009: D/TilesManager(32455): Starting TG #0, 0x651f1f78
02-04 16:33:55.009: D/TilesManager(32455): new EGLContext from framework: 64df0f40
02-04 16:33:55.009: D/GLWebViewState(32455): Reinit shader
我得到的是空白屏幕,而不是epub的内容!我该如何解决这个问题?
答案 0 :(得分:1)
使用此
public class MainActivity extends Activity {
WebView webview;
String line, line1 = "", finalstr = "";
int i = 0;
Book book;
String linez;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView) findViewById(R.id.tv);
AssetManager assetManager = getAssets();
try {
// find InputStream for book
InputStream epubInputStream = assetManager
.open("widget.epub");
// Load Book from inputStream
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());
}
Spine spine = book.getSpine();
List<SpineReference> spineList = spine.getSpineReferences() ;
int count = spineList.size();
TextView tv=new TextView(getApplicationContext());
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");
}
@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", "");
}
}