简体   繁体   English

如何使用图像和内容打开epub文件,当我们单击内容然后移至特定部分时

[英]How to open epub files with Images and contents and when we click on contents then it move to particular part

i want to open ebub files either in webview or by something else but content click and images,videos should be there 我想无论是在打开ebub文件webview或别的东西,但内容点击图片,视频应该有

-I have tried using webview but images cant display in that -我尝试过使用webview但是图像无法显示

        textView.setText(Html.fromHtml(data, new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(String source) {
                String imageAsStr = source.substring(source.indexOf(";base64,") + 8);
                byte[] imageAsBytes = Base64.decode(imageAsStr, Base64.DEFAULT);
                Bitmap imageAsBitmap = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);

                int imageWidthStartPx = (pxScreenWidth - imageAsBitmap.getWidth()) / 2;
                int imageWidthEndPx = pxScreenWidth - imageWidthStartPx;

                Drawable imageAsDrawable = new BitmapDrawable(getResources(), imageAsBitmap);
                imageAsDrawable.setBounds(imageWidthStartPx, 0, imageWidthEndPx, imageAsBitmap.getHeight());
                return imageAsDrawable;
            }
        }, null));

-i tried this also but in this images are display but content click is no there -i也尝试过此操作,但在此图像中显示,但没有内容单击

Use this code provided you have necessary libraries and sampleepubfile.epub in your assets... 如果您的资产中有必要的库和sampleepubfile.epub,请使用此代码...

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.TOCReference;
import nl.siegmann.epublib.epub.EpubReader;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.webkit.WebView;
public class EPubDemo extends Activity {
WebView webview;
String line, line1 = "", finalstr = "";
int i = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    webview = (WebView) findViewById(R.id.webview);
    AssetManager assetManager = getAssets();
    try {
        // find InputStream for book
        InputStream epubInputStream = assetManager
                .open("sampleepubfile.epub");

        // Load Book from inputStream
        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 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());
    }

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

@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", "");
}
}

I have tried this and its working for me 我已经尝试过了,它对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM