在WebView中打开PDF文件 [英] Opening a PDF file inside a WebView

查看:151
本文介绍了在WebView中打开PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大约2天,我试图用自定义的WebvView打开PDF文件.这是我的WebView代码:

Around 2 days I tried to open PDF files in my custom WebvView. Here's my WebView code:

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class WebActivity extends AppCompatActivity {
    public
    WebView mywebViewfull;
    ProgressBar progressBar;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        String url = getIntent().getStringExtra("url");
        mywebViewfull = (WebView) findViewById(R.id.webnav1);
        progressBar=(ProgressBar)findViewById(R.id.progressbar1);
        WebSettings webSettings= mywebViewfull.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setPluginState(WebSettings.PluginState.ON);
        webSettings.setGeolocationEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setSaveFormData(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setGeolocationDatabasePath(getFilesDir().getPath());
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        mywebViewfull.loadUrl(url);
        mywebViewfull.setWebViewClient(new mywebViewfullClient());


    }


    @Override
    public void onBackPressed() {
        if(mywebViewfull.canGoBack())
        {
            mywebViewfull.goBack();
        }

        else
        {
            super.onBackPressed();
        }
    }
    class mywebViewfullClient extends WebViewClient {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        progressBar.setVisibility(View.VISIBLE);
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
       // android.support.v7.app.ActionBar actionBar = getSupportActionBar();
       // actionBar.hide();
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        progressBar.setVisibility(View.GONE);
    }
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url != null && url.startsWith("http://")) {
            //open url contents in webview
            return false;
        }if (url != null && url.startsWith("https://")) {
                //open url contents in webview
            return false;
              //this must open pdf file
        }if (url.endsWith(".pdf")) {
            mywebViewfull.loadUrl("https://docs.google.com/gview?embedded=true&url="+getIntent().getStringExtra("url"));
            return false;

        }
        else {
            //external links in app
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;

        }
    }
}
    //back previous page
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction()==KeyEvent.ACTION_DOWN){
            switch (keyCode){
                case KeyEvent.KEYCODE_BACK:
                    if (mywebViewfull.canGoBack()) {
                        mywebViewfull.goBack();
                    }
                    else  {
                        finish();
                    }
                    return true;
            }
        }
        return super.onKeyDown(keyCode, event);


    }

}

所以...当我尝试打开

So... When I try to open pdf document, nothing happens in my app.

我看不出有什么问题!谁能帮我吗?

I can't see what's wrong! Can anyone help me?

推荐答案

在Android中没有本机支持打开PDF文件,但是您有一些变通办法可以或多或少地为您完成这项工作.替代方法:

There is no native support to open PDF files in Android, but you have some workarounds that can more or less do the job for you. Alternatives:

1-您可以打开系统默认应用程序,以便通过意图从应用程序中读取pdf文件;

1 - You could open your system default app to read the pdf file by an intent from your app;

2-您可以使用第三方库在应用内打开pdf;

2 - You could use a third party library to open the pdf inside your app;

3-您可以调用Google文档网站功能以在Web视图中打开pdf文件.为此,请使用以下代码示例:

3 - You could invoke Google Docs website functionality to open your pdf file inside your webview. To do so, use the following code sample:

String pdfUrl = "http://yourwebsite.com/yourfile.pdf";
String url = "http://docs.google.com/gview?embedded=true&url=" + pdfUrl;
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);

这篇关于在WebView中打开PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆