Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中epub的webview中未显示的图像_Android_Webview_Epub - Fatal编程技术网

android中epub的webview中未显示的图像

android中epub的webview中未显示的图像,android,webview,epub,Android,Webview,Epub,我使用webview在android中显示epub的内容。但是现在我可以显示内容,但不能显示图像。有人能帮我解决这个问题吗 我用来显示的代码是: public class MainActivity extends Activity { WebView webview; String line, line1 = "", finalstr = ""; int i = 0; Book book; String linez; /** Called when the activity is first c

我使用webview在android中显示epub的内容。但是现在我可以显示内容,但不能显示图像。有人能帮我解决这个问题吗

我用来显示的代码是:

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) {

                Log.v("line" + i, Html.fromHtml(line).toString());
                line1 = line1.concat(Html.fromHtml(line).toString());
            }
            finalstr = finalstr.concat("\n").concat(line1);

            i++;
        } catch (IOException e) {

        }

        logTableOfContents(tocReference.getChildren(), depth + 1);
    }

    webview.loadDataWithBaseURL("", finalstr, "text/html", "UTF-8", "");
}
}
公共类MainActivity扩展活动{
网络视图;
字符串行,line1=“”,finalstr=“”;
int i=0;
书籍;
字符串linez;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview=(webview)findviewbyd(R.id.tv);
AssetManager AssetManager=getAssets();
试一试{
//查找书籍的输入流
InputStream epubInputStream=assetManager
.open(“widget.epub”);
//从inputStream加载图书
book=(新的EpubReader()).readEpub(epubInputStream);
//记录这本书的作者
Log.i(“author”,:“+book.getMetadata().getAuthors());
//记录书名
Log.i(“title”,:“+book.getTitle());
/*记录书籍的封面图像属性*/
位图coverImage=BitmapFactory.decodeStream(book.getCoverImage().getInputStream());
Log.i(“epublib”,“Coverimage是”+Coverimage.getWidth()+”乘以“+Coverimage.getHeight()+”像素”);
//记录内容的故事
内容日志表(book.getTableOfContents().gettoReferences(),0);
}捕获(IOE异常){
Log.e(“epublib异常”,e.getMessage());
}
Spine Spine=book.getSpine();
List spineList=spine.getSpineReferences();
int count=spineList.size();
TextView tv=新的TextView(getApplicationContext());
tv.setText(Integer.toString(count));
StringBuilder字符串=新的StringBuilder();
对于(int i=0;计数>i;i++){
Resource 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();
}
}
loadData(linez,“text/html”、“utf-8”);
}
@抑制警告(“未使用”)
内容的私有void日志表(列表引用,int深度){
if(tocpreferences==null){
返回;
}
for(TOCReference TOCReference:tocReferences){
StringBuilder tocString=新的StringBuilder();
for(int i=0;i
用于在webview中显示图像和css。您需要在SD卡的相应目录位置下载css和图像。下面是一些代码

private void DownloadResource(String directory) {
    try {
        nl.siegmann.epublib.domain.Resources rst = book.getResources();
        Collection<Resource> clrst = rst.getAll();
        Iterator<Resource> itr = clrst.iterator();
        Log.d("Downlod path", directory);
        while (itr.hasNext()) {
            Resource rs = itr.next();
            if ((rs.getMediaType() == MediatypeService.JPG) || (rs.getMediaType() == MediatypeService.PNG) || (rs.getMediaType() == MediatypeService.GIF)
                    || (rs.getMediaType() == MediatypeService.CSS)) {
                File oppath1 = new File(directory + File.separator + rs.getHref());
                oppath1.getParentFile().mkdirs();

                Log.d("Resource Name - ", rs.getHref());
                oppath1.createNewFile();
                Log.d("Oppath - ", oppath1.getAbsolutePath());

                Log.d("File Checking - ", "Exists - " + oppath1.exists() + " & Write - " + oppath1.canWrite());
                FileOutputStream fos1 = new FileOutputStream(oppath1);
                fos1.write(rs.getData());
                fos1.close();

            } 

        }
    } catch (IOException e) {
        Log.e("error", e.getMessage());
    }
}
私有void下载资源(字符串目录){
试一试{
nl.siegmann.epublib.domain.Resources rst=book.getResources();
集合clrst=rst.getAll();
迭代器itr=clrst.Iterator();
Log.d(“Downlod路径”,目录);
while(itr.hasNext()){
资源rs=itr.next();
如果((rs.getMediaType()==MediatypeService.JPG)| |(rs.getMediaType()==MediatypeService.PNG)| |(rs.getMediaType()==MediatypeService.GIF)
||(rs.getMediaType()==MediatypeService.CSS)){
File oppath1=新文件(directory+File.separator+rs.getHref());
oppath1.getParentFile().mkdirs();
Log.d(“资源名-”,rs.getHref());
oppath1.createNewFile();
Log.d(“Oppath-”,oppath1.getAbsolutePath());
Log.d(“文件检查-”,“存在-”+oppath1.Exists()+“&Write-”+oppath1.canWrite());
FileOutputStream fos1=新的FileOutputStream(oppath1);
fos1.write(rs.getData());
fos1.close();
} 
}
}捕获(IOE异常){
Log.e(“error”,e.getMessage());
}
}