In my app you can download some files. I used the Android DownloadManager
class for downloading. After the download is completed, it should show me a message that the file was downloaded. The problem is, there could be 2,3 or 4 downloads at the same time. My BroadcastReceiver
code looks like this:
receiver_complete = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE) ){
Toast.makeText(MainActivity.this, MainActivity.this.getString(R.string.download_finished, "Here should be the name", Toast.LENGTH_SHORT).show();
}
}
};
How can I get the current filename of the finished download?
Thank you very much.
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". This Cursor
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.
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/
u try this code to get the file name
DownloadManager.COLUMN_LOCAL_FILENAME
i am not sure about it