安卓下载管理器获取文件名

27

在我的应用中,您可以下载一些文件。我使用了 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();
                }
         }
     };

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

非常感谢。

6个回答

22

Ian Shannon的答案是完全正确的,但我建议做一些改进:

  • 记得在使用完后关闭那个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();

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


5
请注意,COLUMN_LOCAL_FILENAME已经过时。您可以使用COLUMN_LOCAL_URI获取一个Uri,该Uri指向实际文件的位置。 - Paolo Casciello

21

我认为你想把类似这样的内容放在你的if块中。用你的DownloadManager实例替换YOUR_DM

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
    }
}

我该如何在BroadcastReceiver中使用这段代码? - Swapnil

1

有一种更简单的方法来检索已下载文件的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事件的完整说明中找到了这种方法这里


1

0
实际上在2023年
import android.net.Uri;
import android.webkit.URLUtil;

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
String filename = URLUtil.guessFileName(url, contentDisposition, mimeType);

从URL解析中找不到,但是从中接收到的
Content-Disposition: attachment; filename="myDocument.FileName"

-1

您可以尝试使用以下代码获取文件名

DownloadManager.COLUMN_LOCAL_FILENAME

我不确定它


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接