获取DownloadManager当前完成的下载路径

时间:2014-04-09 01:51:40

标签: android download

我的问题是,我需要在完成下载后返回完成的下载文件以执行一些命令。

如果我这样做

doDownload("www.google.com","google1.html");
doDownload("www.google.com","google2.html");

Log.d("ainfo", uriString);仍然保留返回第二个下载的名称

output of LogCat:

D/ainfo(27946): file:///storage/emulated/0/test/google2.html
D/ainfo(27946): file:///storage/emulated/0/test/google2.html

为什么不google1.html和google2.html?这样我就无法按顺序操作文件:(

下载代码:

BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
            long downloadId = intent.getLongExtra(
                    DownloadManager.EXTRA_DOWNLOAD_ID, 0);
            Query query = new Query();
            query.setFilterById(enqueue);
            Cursor c = dm.query(query);
            if (c.moveToFirst()) {
                //      while(i < c.getColumnCount()) {
            //      Log.d("Ainfo", c.getColumnName(i) + "---" + c.getString(i));
                //  i++;

                int columnIndex = c
                        .getColumnIndex(DownloadManager.COLUMN_STATUS);
                if (DownloadManager.STATUS_SUCCESSFUL == c
                    .getInt(columnIndex)) {
                     String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

                     Log.d("ainfo", uriString);

                    //AlertDialog.Builder builder = new AlertDialog.Builder(main);
                    //builder.setTitle("Download(s) Efetuado(s)");


                    //StringBuilder sb = new StringBuilder();
                    //sb.append("/n");
                    //builder.setMessage(sb.toString());
                    //alerta = builder.create();
                    //alerta.show();


                }
            }
        }
    }
};


public void doDownload(String link, String filename) {
    this.dm = (DownloadManager) main.getSystemService(main.DOWNLOAD_SERVICE);


     File direct = new File(Environment.getExternalStorageDirectory()
                + subDirHTMLs);

        if (!direct.exists()) {
            direct.mkdirs();
        }

        File arquivoexistente = new File(Environment.getExternalStorageDirectory()
                + subDirHTMLs + filename);

       if(!arquivoexistente.exists()) {
    Request request = new Request(
            Uri.parse(link));
    enqueue = dm.enqueue(request.setAllowedNetworkTypes(
            DownloadManager.Request.NETWORK_WIFI
            | DownloadManager.Request.NETWORK_MOBILE)
    .setAllowedOverRoaming(false).setTitle(filename)
    .setDescription("Something useful. No, really.")
    .setDestinationInExternalPublicDir(subDirHTMLs, filename));
       } else {
           arquivoexistente.delete();
           doDownload(link, filename);
      }

}

1 个答案:

答案 0 :(得分:1)

在检查SUCCESSFUL

之后解决添加此问题
 if(downloadId == c.getInt(0)) {
                            Log.d("ainfo", c.getString(c.getColumnIndex("local_uri")));

                     }

所以现在我可以进入下一步!!