The file cannot be downloaded completely using the download manager

I was trying to download a file (can be any file) to the browser using a direct link. I've tried with this:

private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webView = (WebView) findViewById(R.id.webView1);
    this.webView.getSettings().setSupportZoom(false);
    this.webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    this.webView.loadUrl("https://drive.google.com/uc?id=0Bw6vr2LNxB3iUFJrTk5oZDljaTA&export=download");

    this.webView.setWebViewClient(new WebViewClientDemo());

}

private class WebViewClientDemo extends WebViewClient implements DownloadListener {
    @Override
    public boolean shouldOverrideUrlLoading(WebView v, String u) {
        v.loadUrl(u);
        v.setDownloadListener(this);
        return true;
    }

    @Override
    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {

        Request request = new Request(
                Uri.parse(url));
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download.png"); 
        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        dm.enqueue(request);

        Toast.makeText(getApplicationContext(), "end", Toast.LENGTH_SHORT).show();

    }
}

      

My link is a direct download link that only works in a browser, so I tried to download it using a webview. But the file is not fully downloaded. For example, in the link my image size is 31KB, but 3/4KB is loaded. Maybe it is just loading the html page because when I open the loaded image in webview it shows the html page. What is the problem that I didn't understand? How can I solve this problem?

+3


source to share





All Articles