Android DownloadManager get filename
I think what you are looking for is the DownloadManager.COLUMN_TITLE
Here is a link to the Android Documents: http://developer.android.com/reference/android/app/DownloadManager.html#COLUMN_TITLE
And here is a tutorial that will explain more than just getting the title. It is for downloading an image from a URL and then displaying it in an application. Mighty useful I think, overall speaking.
http://wptrafficanalyzer.in/blog/downloading-an-image-from-an-http-url-using-downloadmanager-and-displaying-in-imageview-by-dynamically-registered-broadcastreceiver/
There's a simpler way to retrieve the downloaded file URI, and that is with getUriForDownloadedFile
:
download_receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle b = intent.getExtras();
DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
long file_id = b.getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
Uri uri = dm.getUriForDownloadedFile(downloaded_file_id);
}
}
I found this method here in a full description of how to use a BroadcastReceiver for handling DOWNLOAD_COMPLETE events.
I think you want to put something like this inside your if
block. Replace YOUR_DM
with your DownloadManager instance.
Bundle extras = intent.getExtras();
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID));
Cursor c = YOUR_DM.query(q);
if (c.moveToFirst()) {
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.STATUS_SUCCESSFUL) {
// process download
title = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));
// get other required data by changing the constant passed to getColumnIndex
}
}
Ian Shannon was totally right with his answer, but I sugest some improvement:
Remember to close that
Cursor
after using it, avoiding "Cursor Leaking". ThisCursor
consumes a lot of resources and must be released as soon as possible.If you put some title for the download, such as:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.setTitle("Some title");
The value given by the
DownloadManager.COLUMN_TITLE
will be"Some title"
instead of the file name. So I would recommend this instead:String filePath = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME)); title = filePath.substring( filePath.lastIndexOf('/')+1, filePath.length() );
The
COLUMN_LOCAL_FILENAME
returns the entire path (/storage/sdcard0/Android/data/.../filename.ext
), but with this code, we will only get the file name.
Final code:
Bundle extras = intent.getExtras();
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID));
Cursor c = YOUR_DM.query(q);
if (c.moveToFirst()) {
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.STATUS_SUCCESSFUL) {
String filePath = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
filename = filePath.substring( filePath.lastIndexOf('/')+1, filePath.length() );
}
}
c.close();
Edit: Replace YOUR_DM with your DownloadManager instance.