Mohammad Reza Majid Pour Mohammad Reza Majid Pour - 26 days ago 10
Android Question

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

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?

Answer

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