I need to download a file from a URL except I don't know what type the file will be and the URL I am using doesn't have /random.file at the end of it so I can't parse the url for the filename. At the moment I'm using the Android download manager which works greats and means I don't have handle the download but I can't see anyway to get the file name out of the file its downloading. If I load the same url in Firefox for example it asks 'Download file: Nameoffile.extension'.
Is there a way for me to replicate this behaviour and get the file name before I download the file?
You are better off reading the HTTP
Content-Type
header on the response and figuring out what type of file it is. File name extensions do not guarantee the type of the file.Content-Disposition: attachment; filename="fname.ext"
is another way to do this if you are specific about the file name. Check out the list of HTTP headers for more information.I ended up using an ASyncTask to manually retrieve the file name and passing it to the download manager, if it helps anyone this is how I did it (my url went through a number of redirects before the actual file download):