我想在webview上显示pdf内容.这是我的代码:
WebView webview = new WebView(this); setContentView(webview); webview.getSettings().setJavaScriptEnabled(true); webview.loadUrl("http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf");
我得到一个空白的屏幕.我也设置了互联网许可.
您可以使用Google文档查看器在线阅读您的pdf:
WebView webview = (WebView) findViewById(R.id.webview); webview.getSettings().setJavaScriptEnabled(true); String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf"; webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
如果您使用仅查看网址,则不建议用户登录Google帐户.
https://docs.google.com/viewer?url=http://my.domain.com/yourPdfUrlHere.pdf
在用户体验方面,使用谷歌文档打开PDF是一个坏主意.它真的很慢而且没有反应.
从api 21开始,我们有了PdfRenderer,它有助于将pdf转换为Bitmap.我从来没用过它,但看起来很容易.
其他解决方案是下载PDF并通过Intent将其传递给专用的PDF应用程序,该应用程序将执行显示它的banger工作.快速和良好的用户体验,特别是如果此功能不是您的应用程序的核心.
使用此代码下载并打开PDF
public class PdfOpenHelper { public static void openPdfFromUrl(final String pdfUrl, final Activity activity){ Observable.fromCallable(new Callable() { @Override public File call() throws Exception { try{ URL url = new URL(pdfUrl); URLConnection connection = url.openConnection(); connection.connect(); // download the file InputStream input = new BufferedInputStream(connection.getInputStream()); File dir = new File(activity.getFilesDir(), "/shared_pdf"); dir.mkdir(); File file = new File(dir, "temp.pdf"); OutputStream output = new FileOutputStream(file); byte data[] = new byte[1024]; long total = 0; int count; while ((count = input.read(data)) != -1) { total += count; output.write(data, 0, count); } output.flush(); output.close(); input.close(); return file; } catch (IOException e) { e.printStackTrace(); } return null; } }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber () { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(File file) { String authority = activity.getApplicationContext().getPackageName() + ".fileprovider"; Uri uriToFile = FileProvider.getUriForFile(activity, authority, file); Intent shareIntent = new Intent(Intent.ACTION_VIEW); shareIntent.setDataAndType(uriToFile, "application/pdf"); shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); if (shareIntent.resolveActivity(activity.getPackageManager()) != null) { activity.startActivity(shareIntent); } } }); }
}
要使Intent起作用,您需要创建一个FileProvider来授予接收应用程序打开文件的权限.
以下是您如何实现它:在您的清单中:
最后在资源foler中创建一个file_paths.xml文件
希望这有助于=)
在这里加载progressDialog。需要提供WebClient,否则将强制在浏览器中打开:
final ProgressDialog pDialog = new ProgressDialog(context); pDialog.setTitle(context.getString(R.string.app_name)); pDialog.setMessage("Loading..."); pDialog.setIndeterminate(false); pDialog.setCancelable(false); WebView webView = (WebView) rootView.findViewById(R.id.web_view); webView.getSettings().setJavaScriptEnabled(true); webView.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); pDialog.show(); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); pDialog.dismiss(); } }); String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf"; webView.loadUrl("https://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
使用此代码:
private void pdfOpen(String fileUrl){ webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setPluginState(WebSettings.PluginState.ON); //---you need this to prevent the webview from // launching another browser when a url // redirection occurs--- webView.setWebViewClient(new Callback()); webView.loadUrl( "http://docs.google.com/gview?embedded=true&url=" + fileUrl); } private class Callback extends WebViewClient { @Override public boolean shouldOverrideUrlLoading( WebView view, String url) { return (false); } }