Android DownloadManager 不将下载的文件存储在下载文件夹中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24228917/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:54:26  来源:igfitidea点击:

DownloadManager not storing Downloaded files in Download Folder

androiddownload-managerandroid-download-manager

提问by architjn

Whenever i try to download any file through the code below

每当我尝试通过下面的代码下载任何文件时

dm = (DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);
request = new Request(
    Uri.parse(finalurl));
enqueue = dm.enqueue(request);

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()) {
                    int columnIndex = c
                            .getColumnIndex(DownloadManager.COLUMN_STATUS);
                    if (DownloadManager.STATUS_SUCCESSFUL == c
                            .getInt(columnIndex)) {

                        Toast.makeText(context, "download finished", Toast.LENGTH_LONG).show();
                    }
                }
            }
        }
    };

    context.registerReceiver(receiver, new IntentFilter(
            DownloadManager.ACTION_DOWNLOAD_COMPLETE));

The file downloaded shows in Download Manager Application and can be played from there any time but that is not storing the downloaded file in Downloads folder.

下载的文件显示在下载管理器应用程序中,可以随时从那里播放,但不会将下载的文件存储在下载文件夹中。

If i use

如果我使用

.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "filename.extention"));

i get the same result.

我得到相同的结果。

My question is- Where are my downloads going and how can i bring them to downloads folder?

我的问题是 - 我的下载去哪里了,我怎样才能把它们带到下载文件夹?

采纳答案by Nitesh Kumar

To see the downloaded files, you must have File Manager app installed in your phone. Steps to view downloaded files:

要查看下载的文件,您必须在手机中安装文件管理器应用程序。查看下载文件的步骤:

  1. Open File Manager app.
  2. Go to storage -> sdcard
  3. Go to Android -> data -> "Your package name" eg. com.xyx.abc
  4. Here are all your downloads.
  1. 打开文件管理器应用程序。
  2. 转到存储 - > SD卡
  3. 转到 Android -> 数据 -> “您的包名称”,例如。com.xyx.abc
  4. 这是你所有的下载。

Path is: storage/sdcard/Android/data/"your package"

路径为:storage/sdcard/Android/data/“你的包”

Use below methos to save files in Download folder

使用以下方法将文件保存在下载文件夹中

.setDestinationInExternalFilesDir(this, dir, "abc.png");

回答by Fatih Ergin

Try

尝试

request.setDestinationInExternalPublicDir("/folder","file.ext");

This will save the file to

这会将文件保存到

Environment.getExternalStorageDirectory() + "/folder"

回答by Apostolos

If you try to find where the DownloadManager stored the file using

如果您尝试使用以下方法查找 DownloadManager 存储文件的位置

String file = <Cursor>.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)

you get a ficticious path: "context://downloads/my_downloads/{number}". To see the actual path, use:

你会得到一个虚构的路径:“context://downloads/my_downloads/{number}”。要查看实际路径,请使用:

String file = <Cursor>.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME)

You then get: "/data/user/0/com.android.providers.downloads/cache/{filename}" (or something similar) and you can access the file from there.

然后你会得到:“/data/user/0/com.android.providers.downloads/cache/{filename}”(或类似的东西),你可以从那里访问文件。

回答by Maisterino

Steps to find your downloads and export them to local folders. Download Manager - > Top right there is a menu button - > files - > mark the item(s) - > export.

查找下载并将其导出到本地文件夹的步骤。下载管理器 - > 右上角有一个菜单按钮 - > 文件 - > 标记项目 - > 导出。

Hope it helped :)

希望它有所帮助:)