在应用程序

时间:2018-03-07 08:53:39

标签: android pdf webview

您好我尝试使用webview显示pdf。我正确设置了所有代码,但pdf没有打开。但我给了一个网页链接它很好。什么是我的错误我是新手抱歉。

WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_giris);
    wv = (WebView) findViewById(R.id.wv);
    WebSettings webSettings = wv.getSettings();
    wv.setWebViewClient(new WebViewClient());
    wv.getSettings().setBuiltInZoomControls(true); //zoom yapılmasına izin verir
    wv.getSettings().setSupportZoom(true);
   // wv.getSettings().setJavaScriptEnabled(true);
    wv.getSettings().setJavaScriptEnabled(true);

    materialDesignFAM = (FloatingActionMenu) findViewById(R.id.social_floating_menu);
    floatingActionButton1 = (FloatingActionButton) findViewById(R.id.floating_facebook);
    floatingActionButton2 = (FloatingActionButton) findViewById(R.id.floating_twitter);
    floatingActionButton3 = (FloatingActionButton) findViewById(R.id.floating_linkdin);
    floatingActionButton4 = (FloatingActionButton) findViewById(R.id.floating_google_plus);
    floatingActionButton5 = (FloatingActionButton) findViewById(R.id.floating_instagram);
    floatingActionButton6 = (FloatingActionButton) findViewById(R.id.floating_youtube);

    floatingActionButton1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
           String fullPath = "http://aciltip.medicine.ankara.edu.tr/files/2015/09/2015_d45_acil_tip_hizmetleri.pdf";
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fullPath));
          startActivity( browserIntent);
          //  wv.loadUrl("https://docs.google.com/gview?embedded=true&url=http://aciltip.medicine.ankara.edu.tr/files/2015/09/2015_d45_acil_tip_hizmetleri.pdf");
          //  String url = "http://www.example.com";
           // startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        }
    });
    floatingActionButton2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            wv.loadUrl("http://docs.google.com/viewer?embedded=true&url=http://www.ctf.edu.tr/stek/pdfs/70/7001.pdf");
            wv.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return false;
                }
            });
        }
    });
    floatingActionButton3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            wv.loadUrl("http://docs.google.com/gview?embedded=true&url=http://www.ctf.edu.tr/stek/pdfs/70/7001.pdf");

        }
    });

and logcat is these: 
  

03-07 08:50:26.577 3489-3547 / com.er.ap D / EGL_emulation:   eglCreateContext:0xa3539fa0:maj 2 min 0 rcv 2 03-07 08:50:26.578   3489-3547 / com.er.ap D / EGL_emulation:eglMakeCurrent:0xa3539fa0:ver 2   0 03-07 08:50:27.446 3489-3489 / com.er.ap I / chromium:[INFO:CONSOLE(0)]   "内容 - 安全 - 政策指令' base-uri'实施   在目前被禁用的旗帜后面。                                                              ",来源:https://accounts.google.com/o/oauth2/postmessageRelay?parent=https%3A%2F%2Fdocs.google.com&jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.en_US.dLR0UQgpDEo.O%2Fm%3D__features__%2Fam%3DAQE%2Frt%3Dj%2Fd%3D1%2Frs%3DAGLTcCMdWrzahDTQIubih7dySWJqcBU_nw#rpctoken=580516055&forcesecure=1   (0)

但是下面的工作效果很好,但它会退出我的应用程序并转到pdf网址和我的后退按钮消失。如果有人帮我这个问题。这对我来说非常有用

floatingActionButton1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
           String fullPath = "https://drive.google.com/open?id=0B16f9Z9VbJmCc3RhcnRlcl9maWxl";
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fullPath));
          startActivity( browserIntent);
                  });

6 个答案:

答案 0 :(得分:0)

解决方案: -

您可以通过将网址附加到:

在Google文档查看器中打开PDF

http://docs.google.com/gview?embedded=true&url= 这将在默认浏览器或WebView中打开PDF。

您可以使用此格式。

https://docs.google.com/viewerng/viewer?url=http://yourfile.pdf

只需将http://yourfile.pdf替换为您使用的链接。

答案 1 :(得分:0)

使用此代码有帮助,您可以通过内置浏览器或webview中的两种方式下载或查看文件: -

floatingActionButton3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://docs.google.com/gview?embedded=true&url=http://www.ctf.edu.tr/stek/pdfs/70/7001.pdf"));
startActivity(browserIntent);

        }
    });

代表webview: -

WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true); 
String pdf = "http://aciltip.medicine.ankara.edu.tr/files/2015/09/2015_d45_acil_tip_hizmetleri.pdf";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);

答案 2 :(得分:0)

试试这个,

webview = (WebView) findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("https://docs.google.com/viewer?embedded=true&url=http://www.ctf.edu.tr/stek/pdfs/70/7001.pdf");
webview.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return false;
    }
});

答案 3 :(得分:0)

尝试启用JavaScript。

 Webview webView = (WebView) findViewById(R.id.webView1);
 webView.getSettings().setJavaScriptEnabled(true);
 webView.loadUrl(pdf_url);

答案 4 :(得分:0)

您需要将pdf网址附加到您出错的Google Doc Viewer

所以我创建了单独的变量来存储pdf网址,然后将其传递给查看者,请看下面的代码

floatingActionButton3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String pdf = "http://www.ctf.edu.tr/stek/pdfs/70/7001.pdf"; //your pdf address
                wv.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf); //simply append pdf address to doc viewer
            }
        });

答案 5 :(得分:0)

请注明以下代码

public class WebSample extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web);

    WebView mWebview = (WebView) findViewById(R.id.web);
    WebSettings webSettings = mWebview.getSettings();
    webSettings.setJavaScriptEnabled(true);

    mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript

    final Activity activity = this;

    mWebview.setWebViewClient(new WebViewClient() {
        @SuppressWarnings("deprecation")
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
        }
        @TargetApi(android.os.Build.VERSION_CODES.M)
        @Override
        public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
            // Redirect to deprecated method, so you can use it in all SDK versions
            onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
        }
    });

    String pdf = "http://aciltip.medicine.ankara.edu.tr/files/2015/09/2015_d45_acil_tip_hizmetleri.pdf";
    mWebview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);


}

enter image description here