首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

WebView加载网络PDF(1)

2013-03-12 
WebView加载网络PDF(一)main.xml如下:RelativeLayout xmlns:androidhttp://schemas.android.com/apk/re

WebView加载网络PDF(一)

main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >       <WebView        android:id="@+id/webView"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true" /></RelativeLayout>


MainActivity如下:

package c.c;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.net.Uri;import android.os.Bundle;import android.webkit.DownloadListener;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.Button;/** * Demo描述: 利用WebView加载网络PDF资源,并且实现下载 * 步骤: * 1 利用谷歌服务得到解析后的pdf,且在Webview中显示 * 2 实现Webview的下载监听. *  即mWebView.setDownloadListener()实现下载 *   * 备注: * 测试时最好链接VPN */public class MainActivity extends Activity {private WebView mWebView;private Button mButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}private void init() {mWebView = (WebView) findViewById(R.id.webView);loadPDF();}private void loadPDF() {mWebView.getSettings().setJavaScriptEnabled(true);mWebView.getSettings().setSupportZoom(true);mWebView.getSettings().setDomStorageEnabled(true);mWebView.getSettings().setAllowFileAccess(true);mWebView.getSettings().setPluginsEnabled(true);mWebView.getSettings().setUseWideViewPort(true);mWebView.getSettings().setBuiltInZoomControls(true);mWebView.requestFocus();mWebView.getSettings().setLoadWithOverviewMode(true);String pdfUrl = "http://www8.cao.go.jp/okinawa/8/2012/0409-1-1.pdf";mWebView.loadUrl("http://docs.google.com/gview?embedded=true&url="+ pdfUrl);mWebView.setWebViewClient(new WebViewClient() {@Overridepublic void onPageStarted(WebView view, String url, Bitmap favicon) {super.onPageStarted(view, url, favicon);}@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {view.loadUrl(url);return true;}@Overridepublic void onPageFinished(WebView view, String url) {super.onPageFinished(view, url);}@Overridepublic void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {super.onReceivedError(view, errorCode, description, failingUrl);}});mWebView.setDownloadListener(new DownloadListener() {@Overridepublic void onDownloadStart(String url, String userAgent,String contentDisposition, String mimetype,long contentLength) { System.out.println("=========>开始下载 url =" + url); Uri uri = Uri.parse(url);                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);                    startActivity(intent);}});}}


 

热点排行