Android Can';t在屏幕上显示目录或整本书
我正在使用siegmann epub图书馆创建一个图书阅读器。我无法在屏幕上显示任何内容,只能在logcat中显示。要用于显示的布局类型是什么 这是我的main.javaAndroid Can';t在屏幕上显示目录或整本书,android,android-layout,epub,Android,Android Layout,Epub,我正在使用siegmann epub图书馆创建一个图书阅读器。我无法在屏幕上显示任何内容,只能在logcat中显示。要用于显示的布局类型是什么 这是我的main.java public class MainActivity extends Activity { private WebView webview; private String line, line1="", finalstr=""; int i = 0; private String fullBoo
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("\t");
}
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()+
// "\n")).toString();
line1 = line1.concat(Html.fromHtml(line).toString());
}
finalstr = finalstr.concat("\n").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 + "\n").toString();
}
} catch (IOException e) {e.printStackTrace();}
} catch (IOException e) {
e.printStackTrace();
}
}
return linez;
}
}
公共类MainActivity扩展活动{
私有网络视图;
私有字符串行,第1行=”,最后一行=”;
int i=0;
私人字符串完整本;
私人书籍;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview=(webview)findviewbyd(R.id.webview);
AssetManager AssetManager=getAssets();
试一试{
//查找书本的输入流
InputStream epubInputStream=assetManager.open(“oliver_twist.epub”);
//从输入流加载图书
book=(新的EpubReader()).readEpub(epubInputStream);
Log.i(“epublib”,“title:+book.getTitle());
//记录书籍的封面图像属性
//位图coverImage=BitmapFactory.decodeStream(book.getCoverImage().getInputStream());
//Log.i(“epublib”,“CoverImage是”+CoverImage.getWidth()+”乘以“+CoverImage.getHeight()+”像素”);
//记录目录
内容日志表(book.getTableOfContents().gettoReferences(),0);
fullBook=getEntireBook();
}
捕获(IOE异常){
Log.e(“epublib”,e.getMessage());
}
字符串javascrips=“”;
试一试{
//InputStream输入=getResources().openRawResource(R.raw.lights);
InputStream输入=this.getAssets().open(
《爱伦·坡·厄舍之家的倒塌》(epub);
整数大小;
size=input.available();
字节[]缓冲区=新字节[大小];
输入读取(缓冲区);
input.close();
//将字节缓冲区转换为字符串
javascrips=新字符串(缓冲区);
}捕获(IOE异常){
e、 printStackTrace();
}
//字符串html=readFile(is);
webview.loadDataWithBaseURL(“file:///android_asset/“,javascrips,
“应用程序/epub+zip”、“UTF-8”,空);
//webview.setwe
}
内容的私有void日志表(列表引用,int深度){
if(tocpreferences==null){
System.out.println(“--->>>”+tocReferences);
返回;
}
for(TOCReference TOCReference:tocReferences){
StringBuilder tocString=新的StringBuilder();
对于(int i=0;i i;i=i+1){
res=spine.getResource(i);
试一试{
InputStream is=res.getInputStream();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is));
试一试{
而((line=reader.readLine())!=null){
linez=string.append(line+“\n”).toString();
}
}catch(IOE异常){e.printStackTrace();}
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回线Z;
}
}
在main.xml中,我刚刚创建了一个webview。我可以添加什么Other元素?它是一个显示书籍的文本视图吗?我使用以下代码解决了它,它工作了
public class MainActivity extends Activity {
private WebView webview;
private String line, line1="", finalstr="";
int i = 0;
private String fullBook;
private Book book;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
AssetManager assetManager = getAssets();
//tv = (TextView) findViewById(R.id.tv);
try{
//find input Stream for book
InputStream epubInputStream = assetManager.open("TheThreeBears.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);
getEntireBook();
}
catch(IOException e){
Log.e("epublib", e.getMessage());
}
}
private void logTableOfContents(List<TOCReference> tocReferences, int depth){
// Load entire text into particular no. of lines and display each
// then with next button navigate through pages
String lineXX = "p";
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("\t");
}
tocString.append(tocReference.getTitle());
//tv.setText(tocString.toString());
//Log.i("epulib", tocString.toString());
try {
InputStream is = tocReference.getResource().getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
while ((line = r.readLine()) != null) {
//String lineX = Html.fromHtml(line).toString();
//lineXX = lineX;
Log.v("line" + i, Html.fromHtml(line).toString());
line1 = line1.concat(Html.fromHtml(line).toString());
}
finalstr = finalstr.concat("\n").concat(line1);
//tv.setText(finalstr);
//Log.i("Content " + i, finalstr);
i++;
} catch (IOException e) {
}
logTableOfContents(tocReference.getChildren(), depth+1);
}
//return lineXX;
webview.loadDataWithBaseURL("", finalstr, "text/html", "UTF-8", "");
}
public void 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 + "\n").toString();
}
} catch (IOException e) {e.printStackTrace();}
} catch (IOException e) {
e.printStackTrace();
}
}
webview.loadData(linez, "text/html", "utf-8");
}
}
公共类MainActivity扩展活动{
私有网络视图;
私有字符串行,第1行=”,最后一行=”;
int i=0;
私人字符串完整本;
私人书籍;
私家图文电视;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview=(webview)findviewbyd(R.id.webview);
WebSettings WebSettings=webview.getSettings();
setJavaScriptEnabled(true);
webSettings.SetBuilTinZoomControl(真);
AssetManager AssetManager=getAssets();
//tv=(TextView)findviewbyd(R.id.tv);
试一试{
//查找书本的输入流
InputStream epubInputStream=assetManager.open(“TheThreeBears.epub”);
//从输入流加载图书
book=(新的EpubReader()).readEpub(epubInputStream);
Log.i(“epublib”,“title:+book.getTitle());
//记录书籍的封面图像属性
//位图coverImage=BitmapFactory.decodeStream(book.getCoverImage().getInputStream());
//Log.i(“epublib”,“CoverImage是”+CoverImage.getWidth()+”乘以“+CoverImage.getHeight()+”像素”);
//记录目录
内容日志表(book.getTableOfContents().gettoReferences(),0);
getEntireBook();
}
捕获(IOE异常){
Log.e(“epublib”,e.getMessage());
}
}
内容的私有void日志表(列表引用,int深度){
//将整个文本加载到特定数量的行中,并显示每个行
//然后使用“下一步”按钮浏览页面
字符串lineXX=“p”;
if(tocpreferences==null){
System.out.println(“--->>>”+tocReferences);
返回;
}
for(TOCReference TOCReference:tocReferences){
StringBuilder tocString=新的StringBuilder();
对于(int i=0;i i;i=i+1){
res=spine.getResource(i);
试一试{
InputStream is=res.getInputStream();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is));
试一试{
而((line=reader.readLine())!=null){
linez=string.append(line+“\n”).toString();
}
}catch(IOE异常){e.printStackTrace();}
}捕获(IOE异常){
e、 普林斯塔