is it possible to use web view as a html reader (offline)?

ε祈祈猫儿з 提交于 2019-12-23 00:31:09

问题


i was searching for a way to zoom for both text and picture in my layout. according to some questions web view is a easy way to do it. after a while i found this code for webview:

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class WebViewActivity extends Activity {

private WebView webView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://www.google.com");

}

}

i tried to use file:/// instead of http:// so i used this path:

file:///sdcard/Android/data/[My App Package Name]/about.html

it seems its working fine for me, but i have some serious problems.

here is my problems:

  1. is this path i am using is works for all other android devices?
  2. i dont know how to extract html files for the first run in to that path i am using.
  3. is there anyway to load the files from assets?
  4. what is the source code for extract files in first run?
  5. can i use other formats like pdf or doc instead of html? if i couldnt what should i change in the source code?

thank guys for helping me. should i give up or continue this idea?


回答1:


You could just put the file into your assets folder in the source code, and then do this:

WebView webView = (WebView)findViewById(R.id.webView1);
webview.loadUrl("file:///android_assets/file.html");

For display PDF:

Since API Level 21 (Lollipop) Android provides a PdfRenderer class:

// create a new renderer
PdfRenderer renderer = new PdfRenderer(getSeekableFileDescriptor());

// let us just render all pages
final int pageCount = renderer.getPageCount();
for (int i = 0; i < pageCount; i++) {
  Page page = renderer.openPage(i);

  // say we render for showing on the screen
  page.render(mBitmap, null, null, Page.RENDER_MODE_FOR_DISPLAY);

  // do stuff with the bitmap

  // close the page
  page.close();
}

// close the renderer
renderer.close();

This sample demonstrates how to display PDF document on screen using the PdfRenderer introduced in Android 5.0 Lollipop.

For older APIs, checkout AndroidPdfViewer library, works on API 11 and higher:

pdfView.fromUri(Uri)
or
pdfView.fromFile(File)
or
pdfView.fromAsset(String)
    .pages(0, 2, 1, 3, 3, 3) // all pages are displayed by default
    .enableSwipe(true)
    .swipeHorizontal(false)
    .enableDoubletap(true)
    .defaultPage(0)
    .onDraw(onDrawListener)
    .onLoad(onLoadCompleteListener)
    .onPageChange(onPageChangeListener)
    .onPageScroll(onPageScrollListener)
    .onError(onErrorListener)
    .enableAnnotationRendering(false)
    .password(null)
    .scrollHandle(null)
    .load();



回答2:


Use getExternalFilesDir() instead of that hard coded path.

Put your files in assets using your IDE.

Then at runtime copy those files to getExternalFilesDir(). Code to do that has been published many times on stackoverflow.

You could leave your files in assets too as WebView can load them from assets directly.



来源:https://stackoverflow.com/questions/40482556/is-it-possible-to-use-web-view-as-a-html-reader-offline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!