Display the pdf file(stored in google drive) in webview by using google docs

前端 未结 5 1075
半阙折子戏
半阙折子戏 2021-01-03 03:39

My pdf file is stored in some website ex: http://www.pdf995.com/samples/pdf.pdf

Now I can\'t rely on other websites for my app. So

  1. I\'ve downloaded th
相关标签:
5条回答
  • 2021-01-03 04:01

    Define Url like:

    String BASE_URL = "https://drive.google.com/viewerng/viewer?embedded=true&url=https://drive.google.com/uc?id=";
    String PDF_ID = "<Your pdf id from Google drive share link>";
    

    Load in WebView:

    webview.loadUrl(BASE_URL + PDF_ID);
    
    0 讨论(0)
  • 2021-01-03 04:07

    I have a WebView, and the link to my pdf stored in google drive is:

    String myPdfUrl = "https://drive.google.com/file/d/1fQfqgEmz-AiCpdHEIh7SNwdnAkQFqDQp/view";
    

    Don't forget "/view".

    And my java code for display:

    private void displayWebView() {
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(myPdfUrl);
                return true;
            }
        });
        webView.loadUrl(myPdfUrl);
    }
    

    And add @SuppressLint("SetJavaScriptEnabled") above onCreate()

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act
    
    0 讨论(0)
  • 2021-01-03 04:08

    I was suffering from same problem and after a lot of hit and trials, I found a way out.

    You need to click on download button of google drive.

    as soon as you click it, a new tab will open with a different url, of this type

    https://drive.google.com/uc?id=0B1dndMpNP4zgdlTuMm5rcjFk3TQ&export=download //test url//

    String url="https://drive.google.com/uc?id=0B1dndMpNP4zgdlTuMm5rcjFk3TQ&export=download";
    webView = (WebView) findViewById(R.id.progr);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url="+url);
    
    0 讨论(0)
  • 2021-01-03 04:09

    First, you need to follow the answer provided by "Phúc Nghi Lâm." (the 3rd answer.)

    If it is not working....

    In my situation, there is HTML-result coming. But your WebView's size is 0-width and 0-height.

    So you need to reset the LayoutParam by implement the "onPageFinished." Or something else.

    (You should be able to get the parent-view of the WebView. So.. 1.Get the size of parent-view. 2.Set the size data into a new LayoutParam. 3. Set the new LayoutParam to the WebView.)

    0 讨论(0)
  • 2021-01-03 04:11

    you can view any pdf on the internet using google docs even without downloading it to the device. Here is the sample of how to do it:

    webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
    

    Where pdf is a string link to your PDF file. So in your case it will be:

    String pdf = "http://www.pdf995.com/samples/pdf.pdf";
    webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
    
    0 讨论(0)
提交回复
热议问题