图像不显示在Android的epub的webview中

时间:2013-02-05 04:41:39

标签: 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 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", "");
}
}

1 个答案:

答案 0 :(得分:1)

用于在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());
    }
}