无法设置Android下载管理器下载文件的位置,也无法获取真实的文件名

无法设置Android下载管理器下载文件的位置,也无法获取真实的文件名,android,android-download-manager,Android,Android Download Manager,我正在开发一个应用程序,并使用内置的Android下载管理器类。我想将文件下载到名为“myApp”的文件夹中(该文件夹已存在)。这就是我试过的。我还尝试了不同的方法来设置下载位置,但都不起作用。请帮助解决此问题 DownloadManager mgr = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE); boolean isDownloading = false; Downl

我正在开发一个应用程序,并使用内置的Android下载管理器类。我想将文件下载到名为“myApp”的文件夹中(该文件夹已存在)。这就是我试过的。我还尝试了不同的方法来设置下载位置,但都不起作用。请帮助解决此问题

  DownloadManager mgr = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);

        boolean isDownloading = false;
        DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterByStatus(
                DownloadManager.STATUS_PAUSED|
                        DownloadManager.STATUS_PENDING|
                        DownloadManager.STATUS_RUNNING|
                        DownloadManager.STATUS_SUCCESSFUL
        );
        Cursor cur = mgr.query(query);
        int col = cur.getColumnIndex(
                DownloadManager.COLUMN_LOCAL_FILENAME);
        for(cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
            isDownloading = isDownloading || ("local file path" == cur.getString(col));
        }
        cur.close();

        if (!isDownloading) {
            Uri source = Uri.parse(myWebsites[j]);

            Uri dst_uri = Uri.parse("file:///mnt/sdcard/Signagee");

            DownloadManager.Request request = new DownloadManager.Request(source);

            request.setDestinationUri(dst_uri);
            request.setNotificationVisibility(
                    DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED
            );
            request.allowScanningByMediaScanner();

            long id = mgr.enqueue(request);
        }
用这个

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
所有下载内容都将在下载文件夹中下载。您可以从Environment类中选择任何其他目标

要获取文件名,请选中此项

String filePath = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
        filename = filePath.substring( filePath.lastIndexOf('/')+1, filePath.length() );
使用上面一行中的文件名

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
所有下载内容都将在下载文件夹中下载。您可以从Environment类中选择任何其他目标

要获取文件名,请选中此项

String filePath = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
        filename = filePath.substring( filePath.lastIndexOf('/')+1, filePath.length() );

在上行中使用此
文件名

不要使用像
/mnt/sdcard/
这样的硬编码路径。使用
Environment
类您可以帮助获取文件名。不要使用像
/mnt/sdcard/
这样的硬编码路径。使用
Environment
类您可以帮助获取文件名。非常感谢。它起作用了。但我仍然无法获得下载文件的真实文件名@萨鲁。请help@Sallu-是的,我也找到了这个答案,并对(cur.moveToFirst();!cur.isAfterLast();cur.moveToNext()){isDownloading=isDownloading | |((“本地文件路径”==cur.getString(col));title=cur.getString(cur.getColumnIndex(DownloadManager.COLUMN| title));String filePath=cur.getString(cur.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));FILENAME=filePath.substring(filePath.lastIndexOf('/')+1,filePath.length());}但我不能在loopdone之外使用“FILENAME”。但是你能帮我写一下标题吗。我添加了一条评论above@venura你在标题中出现了什么错误?你能具体说明吗?让我们来。非常感谢。它起作用了。但我仍然无法获得下载文件的真实文件名@萨鲁。请help@Sallu-是的,我也找到了这个答案,并对(cur.moveToFirst();!cur.isAfterLast();cur.moveToNext()){isDownloading=isDownloading | |((“本地文件路径”==cur.getString(col));title=cur.getString(cur.getColumnIndex(DownloadManager.COLUMN| title));String filePath=cur.getString(cur.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));FILENAME=filePath.substring(filePath.lastIndexOf('/')+1,filePath.length());}但我不能在loopdone之外使用“FILENAME”。但是你能帮我写一下标题吗。我添加了一条评论above@venura你在标题中出现了什么错误?你能具体说明一下吗?让我们看看。