从epub书中获取图像

时间:2014-01-29 03:40:38

标签: android image epub

我已经通过epub api和几乎所有关于此的stackoverflow链接但无法显示epub书的图像。我在allRef: allRef = book.getResources().getAllHrefs();

中有相对的图像路径

但我不知道如何显示它们,用html渲染它们。我是一个初学者,如果这个问题重复,请原谅我,但我已经阅读了有关它的所有链接,没有一个对我有用。 任何建议都会非常有用.. 这是代码。

// mainactivity

public class MainActivity extends Activity implements OnTouchListener{

    private WebView webview;
    private String line, line1="", finalstr="";
    int i = 0;
    private String fullBook;
    private Book book;

    private TextView tv;
    private static int width = 0;
    private static int height =0;

    //Touch attributes
    StringBuffer sb = new StringBuffer();
    private float pressure = 0.0f;
    private float xCoord = 0;
    private float yCoord = 0;
    private int pCount = 0; //pointerCount
    int position = 0;
    Collection<String> allRef;

    private LinearLayout mainLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webview = (WebView) findViewById(R.id.webview);
        webview.setWebChromeClient(new WebChromeClient());


        WebSettings webSettings =  webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        AssetManager assetManager = getAssets();
        String[] files;

        try{
            //find input Stream for book
            InputStream epubInputStream = assetManager.open("TheThreeBears.epub");

            //Load book from input stream
            book = (new EpubReader()).readEpub(epubInputStream);
            allRef =  book.getResources().getAllHrefs();
            Log.i("IMAGETAG",allRef.toString()); //IMAGES ARE PRESENT HERE

            Log.i("epublib", "title: "+book.getTitle());
            log("Hello Three Bears");

            DownloadResource("file:///android_asset/");

            //Log the tables of contents
            logTableOfContents(book.getTableOfContents().getTocReferences(),0);

            getEntireBook();


        }
        catch(Exception e){
            Log.e("epublib", e.getMessage());
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        //width = v.getWidth();
        //height = v.getHeight();
        //Log.i("WIDTH", width+"");
        //Log.i("HEIGHT", height+"");

        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Toast.makeText(getApplicationContext(), "Touch Down x="+x+"  y="+y ,Toast.LENGTH_SHORT).show();
            //Handle Touch Down
            break;
        case MotionEvent.ACTION_MOVE:

            //Handle Touch Move
            break;
        case MotionEvent.ACTION_UP:
            Toast.makeText(getApplicationContext(), "Touch Up x="+x+"  y="+y ,Toast.LENGTH_SHORT).show();
            //Handle Touch Up
            break;
        }
        return false;   
    }

    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


        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());

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

                while ((line = r.readLine()) != null) {

                    line1 = line1.concat(Html.fromHtml(line).toString());

                }
                finalstr = finalstr.concat("\n").concat(line1);
                i++;
            } catch (IOException e) {
                e.printStackTrace();
            }

            logTableOfContents(tocReference.getChildren(), depth+1);
        }
        webview.loadDataWithBaseURL("file:///android_asset/", finalstr, "text/html", "UTF-8", null);
        //webview.loadDataWithBaseURL("file://"+Environment.getExternalStorageDirectory()+"/books/", finalstr, "text/html", "utf-8", null);
        //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");


    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        return false;
    }

}

1 个答案:

答案 0 :(得分:0)