【问题标题】:How To Download PDF File From Google Drive with WebView如何使用 WebView 从 Google Drive 下载 PDF 文件
【发布时间】:2020-01-28 15:49:54
【问题描述】:

我想从我的驱动器链接下载一个文件。 我不知道该怎么做,代码是什么。 URL 是https://drive.google.com/open?id=1txJFXxL9uY3AjyQ3C2G9t9lSkhJqaxuO。 当我尝试这段代码时,我的 WebView 冻结了。

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

    //Webview
    WebView webView = findViewById(R.id.wvnpwpop);
    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl("https://drive.google.com/open?id=1txJFXxL9uY3AjyQ3C2G9t9lSkhJqaxuO");
    webView.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
                                    String contentDisposition, String mimetype,
                                    long contentLength) {
            DownloadManager.Request request = new DownloadManager.Request( Uri.parse(url));
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "myPDFfile.pdf");
            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);
        }
    });
    //WebSettings
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setSupportMultipleWindows(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    webSettings.setSupportMultipleWindows(true);
    // -------- End Of WebView and Web Settings

【问题讨论】:

    标签: java android pdf


    【解决方案1】:

    试试这个并检查:

     webView.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
                    String contentDisposition, String mimetype,
                    long contentLength) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(mUrl));
            startActivity(i);
        }
    });
    

    【讨论】:

    • 我试了,结果还是一样,webView 还是死机,什么都没有显示。
    【解决方案2】:

    您可能需要登录。查找 url 以登录并重定向到您的 gdrive 位置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-04
      • 2020-05-23
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多