java Android DownloadManager 获取文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13321984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android DownloadManager get filename
提问by
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:
在我的应用程序中,您可以下载一些文件。我使用 AndroidDownloadManager
类进行下载。下载完成后,它应该向我显示文件已下载的消息。问题是,可能同时进行 2,3 或 4 次下载。我的BroadcastReceiver
代码如下所示:
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.
非常感谢你。
采纳答案by Ian Shannon
I think you want to put something like this inside your if
block. Replace YOUR_DM
with your DownloadManager instance.
我想你想把这样的东西放在你的if
块里。替换YOUR_DM
为您的 DownloadManager 实例。
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
}
}
回答by Mario Velasco
Ian Shannon was totally right with his answer, but I sugest some improvement:
伊恩·香农 (Ian Shannon) 的回答完全正确,但我建议有一些改进:
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.
Cursor
使用后记得关闭它,避免“光标泄漏”。这Cursor
会消耗大量资源,必须尽快释放。如果您为下载添加了一些标题,例如:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.setTitle("Some title");
给出的值
DownloadManager.COLUMN_TITLE
将"Some title"
代替文件名。所以我会推荐这个:String filePath = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME)); title = filePath.substring( filePath.lastIndexOf('/')+1, filePath.length() );
在
COLUMN_LOCAL_FILENAME
返回的整个路径(/storage/sdcard0/Android/data/.../filename.ext
),但与此代码,我们将只得到文件名。
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.
编辑:将 YOUR_DM 替换为您的 DownloadManager 实例。
回答by Siddharth Lele
I think what you are looking for is the DownloadManager.COLUMN_TITLE
我认为你正在寻找的是 DownloadManager.COLUMN_TITLE
Here is a link to the Android Documents: http://developer.android.com/reference/android/app/DownloadManager.html#COLUMN_TITLE
这是 Android 文档的链接: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.
这是一个教程,不仅可以解释获得标题,还可以解释更多内容。它用于从 URL 下载图像,然后在应用程序中显示它。总的来说,我认为非常有用。
回答by Sean Pianka
There's a simpler way to retrieve the downloaded file URI, and that is with getUriForDownloadedFile
:
有一种更简单的方法来检索下载的文件 URI,那就是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 herein a full description of how to use a BroadcastReceiver for handling DOWNLOAD_COMPLETE events.
我发现这个方法在这里在如何使用用于处理DOWNLOAD_COMPLETE事件广播接收器的完整描述。
回答by Nithinlal
u try this code to get the file name
你试试这个代码来获取文件名
DownloadManager.COLUMN_LOCAL_FILENAME
下载管理器.COLUMN_LOCAL_FILENAME
i am not sure about it
我不确定