Get file name from headers with DownloadManager in Android

回眸只為那壹抹淺笑 提交于 2019-11-30 15:12:42

问题


I'm using DownloadManager to download video files from a url.

The problem is if I use the default folder to download the file I can not see the video in the galery.

Also, If I try to use this method:

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, 'filename');

I need to know the file name before download which in this case, I don't.

And also, I don't have the name of the file in the url.

How can I do to get the file name from the headers and pass the name to the method setDestinationInExternalPublicDir ? Other alternatives?


回答1:


In case anyone want an implementation of doing a HEAD request to get the filename:

class GetFileName extends AsyncTask<String, Integer, String>
{
    protected String doInBackground(String... urls)
    {
        URL url;
        String filename = null;
        try {
            url = new URL(urls[0]);
            String cookie = CookieManager.getInstance().getCookie(urls[0]);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestProperty("Cookie", cookie);
            con.setRequestMethod("HEAD");
            con.setInstanceFollowRedirects(false);
            con.connect();

            String content = con.getHeaderField("Content-Disposition");
            String contentSplit[] = content.split("filename=");
            filename = contentSplit[1].replace("filename=", "").replace("\"", "").trim();
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
        }
        return filename;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected void onPostExecute(String result) {

    }
}



回答2:


Small tip, there is a nice helper method in Android

URLUtil.guessFileName(url, contentDisposition, contentType);

So after completing the call to the server, getting the contenttype and contentDisposition from the Headers this will try and find the filename from the information.




回答3:


I got into the same problem.I used @rodeleon answer but there was no Content-Disposition in response header. Then I analyzed url header from Chrome dev tools and got 'Location' in response header which contained filename at the end, it was like "b/Ol/fire_mp3_24825.mp3". so instead of using

String content = con.getHeaderField("Content-Disposition")

I used

String content = con.getHeaderField("Location") 

and at the end in onPostExecute

    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        String fileName = result.substring(result.lastIndexOf("/") + 1);
        // use result as file name
        Log.d("MainActivity", "onPostExecute: " + fileName);
    }



回答4:


    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MOVIES, uri.getLastPathSegment());


来源:https://stackoverflow.com/questions/23069965/get-file-name-from-headers-with-downloadmanager-in-android

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