0

I'm using DownloadManager class to manage downloads, i'm using this

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,subPath);

currently i'm setting subPath to some filename but how do i get filename from url?

I don't want to use cursor but some way to extract file name from uri.

pintu236
  • 148
  • 2
  • 11

2 Answers2

0

You can use:

DownloadManager.COLUMN_TITLE

Please look at Android DownloadManager get filename

Hope this helps!

Community
  • 1
  • 1
Actiwitty
  • 1,232
  • 2
  • 12
  • 20
0

Well , if no cursor then try this:

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) {

}
}
johnrao07
  • 6,690
  • 4
  • 32
  • 55