DownloadManager not storing Downloaded files in Download Folder

冷暖自知 提交于 2019-12-06 03:20:52

问题


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?


回答1:


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.

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

Use below methos to save files in Download folder

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



回答2:


Try

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

This will save the file to

Environment.getExternalStorageDirectory() + "/folder"



回答3:


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

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

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

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.




回答4:


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 :)



来源:https://stackoverflow.com/questions/24228917/downloadmanager-not-storing-downloaded-files-in-download-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!