0

Hi I try to display pdf with webview. I set all codes properly but pdf didnt open. but I give a webpage link it works good. what is my mistake I am newbie sorry.

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)] "The Content-Security-Policy directive 'base-uri' is implemented behind a flag which is currently disabled. ", source: 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)

But below works good as my will but it quits my apps and go to the pdf url and my back button disapper. if anyone help me this issue. it will be very usefull for me

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);
                  });
a.dem
  • 11
  • 1
  • 7
  • check this on https://stackoverflow.com/a/5296125/2633909 – DKV Mar 07 '18 at 09:05
  • did you find the solution? – DKV Mar 07 '18 at 09:13
  • I edit all my codes there I tried 3 options to display pdf but only first buttton works but it quits my apps and display pdf but I want to display pdf within my apps. thanks – a.dem Mar 07 '18 at 09:16
  • did you try anything? i am able to load the pdf – DKV Mar 07 '18 at 09:39
  • Please check https://stackoverflow.com/a/49148637/2633909 – DKV Mar 07 '18 at 09:43
  • my last code is work have you any idea to set it as my will so I edit my question. ı want that it doesnt quit my apps, when display pdf. – a.dem Mar 07 '18 at 10:30

6 Answers6

1

Try this,

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;
    }
});
Nadun Kulatunge
  • 1,567
  • 2
  • 20
  • 28
  • I edit all my codes there I tried 3 options to display pdf but only first buttton works but it quits my apps and display pdf but I want to display pdf within my apps. thanks – a.dem Mar 07 '18 at 09:14
0

Solution:-

You can open PDF in Google Docs Viewer by appending URL to:

http://docs.google.com/gview?embedded=true&url= This would open PDF in default browser or a WebView.

You can use this format.

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

Just replace http://yourfile.pdf with the link you use.

Mr. Roshan
  • 1,777
  • 13
  • 33
0

Use this code helps, you may download or view the file by two ways like in-built browser or in the 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);

        }
    });

for 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);
Abhinav Gupta
  • 2,225
  • 1
  • 14
  • 30
  • I tried these codes before. it works good but it is not webview. and it cant open in my application. so it quits my apps and goes pdf file. do you know any solution to display pdf within my apps( dont quit my apps so my back button must see below the uı.) thanks your interest – a.dem Mar 07 '18 at 09:03
  • I edit all my codes there I tried 3 options to display pdf but only first buttton works but it quits my apps and display pdf but I want to display pdf within my apps. thanks – a.dem Mar 07 '18 at 09:15
0

Try enabling JavaScript.

 Webview webView = (WebView) findViewById(R.id.webView1);
 webView.getSettings().setJavaScriptEnabled(true);
 webView.loadUrl(pdf_url);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Insane Developer
  • 1,014
  • 10
  • 19
0

You need to append the pdf url to Google Doc Viewer that is where you went wrong

so i created seperate variable to store pdf url and then pass it to viewer see my below code

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
            }
        });
Akshay Katariya
  • 1,464
  • 9
  • 20
0

Please you the following code

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

DKV
  • 1,767
  • 3
  • 28
  • 49
  • thanks but I couldnt. I guess there is another problem. so it seems to be working top of uı. when I click button1, pdf is working background the pagethere is some movement top of the page but thats it only. it doesnt open unfortunately – a.dem Mar 07 '18 at 10:14
  • my last code is work have you any idea to set it as my will so I edit my question – a.dem Mar 07 '18 at 10:29