WebView - can't download file without requesting it twice?

后端 未结 3 1546
感动是毒
感动是毒 2021-01-12 06:08

If I listen with DownloadListener, I get the URL which I need to request after the browser already requested it. The browser already opened a connectio

相关标签:
3条回答
  • 2021-01-12 06:57

    A simple solution is modify it to just download it without asking the user for confirmation based on content type, but instead just put a cancel button on whatever is used to monitor the download.

    0 讨论(0)
  • 2021-01-12 07:00

    Why not just use the url to download it using outputstream? Here is an example:

    private class DownloadFile extends AsyncTask<String, Integer, String> {
    @Override
    protected String doInBackground(String... sUrl) {
        try {
            URL url = new URL(sUrl[0]);
            URLConnection connection = url.openConnection();
            connection.connect();
    
            // download the file
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream("/sdcard/file_name.extension");
    
            byte data[] = new byte[1024];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                total += count;
                output.write(data, 0, count);
            }
    
            output.flush();
            output.close();
            input.close();
        } catch (Exception e) {
        }
        return null;
    }
    
    0 讨论(0)
  • 2021-01-12 07:08

    Are you sure you wish to interrupt browser work? It use multy-thread to download multiple URL's and also he is managing his own file system to create cookies for that urls and he know when he need to delete them and update them.

    So are you sure?

    0 讨论(0)
提交回复
热议问题