Android: How do I download a file from a dynamic url webview

岁酱吖の 提交于 2020-01-25 06:51:08

问题


In my app I am using a webview to navigate through to a site, automatically fill in a web form using javascript then submit to obtain a link to a CSV export file. The link looks like this: XYZ.com/TEST/index/getexport?id=130.

I'd like to download the file this URL points to, then when complete read it into a local database but I'm having trouble downloading the linked file.

If I simply try to open the URL in webview I get an error from the webpage telling me no such file exists.

If I use the Download Manager to download it myself, the source code is downloaded as an html file, not the associated .csv file.

I can open the url with an ACTION_VIEW intent and a browser (chrome) downloads the correct file, but this way I have no notification of when the download completes.

Any ideas of how to download my .CSV file?


回答1:


To download a file from webview use this :

mWebView.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(url));
        startActivity(i); 
    } 
});

Hope this helps.




回答2:


You could resort to manually downloading the file from the url using an AsyncTask.

Here id the background part:

@Override
protected String doInBackground(Void... params) {
    String filename = "inputAFileName";

    HttpURLConnection c;
    try {
        URL url = new URL("http://someurl/" + filename);
        c = (HttpURLConnection) url.openConnection();
        c.setRequestMethod("GET");
        c.setDoOutput(true);
        c.connect();
    } catch (IOException e1) {
        return e1.getMessage();
    }

    File myFilesDir = new File(Environment
            .getExternalStorageDirectory().getAbsolutePath()
            + "/Download");

    File file = new File(myFilesDir, filename);

    if (file.exists()) {
        file.delete();
    }

    if ((myFilesDir.mkdirs() || myFilesDir.isDirectory())) {
        try {
            InputStream is = c.getInputStream();
            FileOutputStream fos = new FileOutputStream(myFilesDir
                    + "/" + filename);

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fos.close();
            is.close();

        } catch (Exception e) {
            return e.getMessage();
        }

        if (file.exists()) {
            return "File downloaded!";
        } else {
             Log.e(TAG, "file not found");
        }
    } else {
        Log.e(TAG, "unable to create folder");
    }
}

Perhaps it would make sense to refactor it so that the file is returned. Then you get the file as an argument in onPostExecute as soon as the download is complete.



来源:https://stackoverflow.com/questions/20070798/android-how-do-i-download-a-file-from-a-dynamic-url-webview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!