2017-01-11 16 views
0

私はそれの中にウェブサイトを読み込む単一のWebViewを使用するアンドロイドアプリケーションを持っています。Android WebviewダウンロードマネージャーPDFファイルをダウンロードする

setContentView(R.layout.activity_main); 
WebView webView = (WebView) this.findViewById(R.id.webview); 

は、その後私は、ウェブサイトのHTMLの代わりに、PDFをダウンロードしてもらうウェブサイトによって動的に作成されたファイルをダウンロードしようとすると、サーバー

webView.setDownloadListener(new DownloadListener() { 

     public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { 
      // ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE); 
      //for downloading directly through download manager 

      DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); 

      request.allowScanningByMediaScanner(); 
      Environment.getExternalStorageDirectory(); 
      getApplicationContext().getFilesDir().getPath(); //which returns the internal app files directory path 
      request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
      request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download"); 
      DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
      dm.enqueue(request); 
     } 
    }); 

から私のファイルをダウンロードするためにダウンロードマネージャを使用しました。 HTMLページは、認証のためのログインページが表示されていることをアドバンス実際

+0

onDownloadListenerに3行を追加する必要がありますか? – Shadow

答えて

1

webView.loadUrl("http://bookboon.com/"); 

感謝。したがって、そのウェブサイトのクッキー内にあるセッションキーが必要です。

だからあなたはあなたが通知を取得することができることができるかどうか

 webView.setDownloadListener(new DownloadListener() { 

       public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { 
        // ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE); 
        //for downloading directly through download manager 

        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); 

       //This three lines will do your work 

        CookieManager cookieManager = CookieManager.getInstance(); 
        String cookie = cookieManager.getCookie("<Your website url>");  // which is "http://bookboon.com" 
        request.addRequestHeader("Cookie", cookie); 
       //................................................ 
        request.allowScanningByMediaScanner(); 
        Environment.getExternalStorageDirectory(); 
        getApplicationContext().getFilesDir().getPath(); //which returns the internal app files directory path 
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download"); 
        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
        dm.enqueue(request); 
       } 
      }); 
+1

API 23+をターゲティングしている場合は、外部デバイスを書き込む許可を確認する必要があります – Sam

関連する問題