2

I am using webview to display content from epub in android.But now i am able to display content but not able to show the images.Could any one help me to solve this issue?

The code i used to display is::

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

1 回答 1

1

用于在 webview 中显示图像和 css。您需要在相应的目录 sdcard 位置下载 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());
    }
}
于 2013-06-20T00:47:53.067 回答