Android DownloadManager获取文件名

时间:2012-11-10 12:18:45

标签: java android broadcastreceiver android-download-manager receiver

在我的应用中,您可以下载一些文件。我使用Android DownloadManager类进行下载。下载完成后,它会显示一条消息,说明该文件已下载。问题是,可能同时有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();
                }
         }
     };

如何获取已完成下载的当前文件名?

非常感谢。

5 个答案:

答案 0 :(得分:20)

我想你想在你的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
    }
}

答案 1 :(得分:16)

伊恩·香农对他的回答是完全正确的,但我有所改善:

  • 请记住在使用它之后关闭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),但使用此代码,我们只会获取文件名。

最终代码:

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();

编辑:将您的DownloadManager实例替换为YOUR_DM。

答案 2 :(得分:1)

我认为你要找的是DownloadManager.COLUMN_TITLE

以下是Android文档的链接:http://developer.android.com/reference/android/app/DownloadManager.html#COLUMN_TITLE

这是一个教程,不仅仅是解释标题。它用于从URL下载图像,然后将其显示在应用程序中。总的来说,我认为有用的是强大的。

http://wptrafficanalyzer.in/blog/downloading-an-image-from-an-http-url-using-downloadmanager-and-displaying-in-imageview-by-dynamically-registered-broadcastreceiver/

答案 3 :(得分:0)

你试试这段代码来获取文件名

DownloadManager.COLUMN_LOCAL_FILENAME

我不确定

答案 4 :(得分:0)

有一种更简单的方法来检索下载的文件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);

    }
}

我在关于如何使用BroadcastReceiver处理DOWNLOAD_COMPLETE事件的完整说明中找到了此方法here