Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DownloadManager double download

I have the following problem: Whenever I download a file with the DownloadManager it is downloaded twice (saved in the fashion "filename.extension" and "filename-1.extension"). Here is my code:

public void download() {
        Request request = new Request(Uri.parse(_wrapper.getURL()));
        request.setTitle(getFileName(_wrapper.getURL()));
        request.setVisibleInDownloadsUi(false);
        request.setDestinationInExternalFilesDir(_context, null, "/" + getFileName(_wrapper.getURL()));

        _downloadID = _downloadManager.enqueue(request);
    }



    public BroadcastReceiver getDownloadFinishedBroadcastReceiver() {
        BroadcastReceiver receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context pContext, Intent pIntent) {
                String action = pIntent.getAction();
                if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                    Query query = new Query();
                    query.setFilterById(_downloadID);
                    Cursor cursor = _downloadManager.query(query);
                    if (cursor.moveToFirst()) {
                        File file = new File(ScruloidConstants.APPLICATION_DIRECTORY);
                        int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
                        if (status == DownloadManager.STATUS_SUCCESSFUL) {
                            String path = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
                            _wrapper.setFilePath(path);
                            _wrapper.setLastDownloaded(new Date());
                            if (_listener != null) {
                                _listener.onDownloadProjectTaskFinished(new TaskResult<ProjectWrapper>(_wrapper));
                            }
                        }
                        else if (status == DownloadManager.STATUS_FAILED) {
                            int reason = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_REASON));
                            DownloadFailedException ex = new DownloadFailedException(reason);
                            if (_listener != null) {
                                _listener.onDownloadProjectTaskFinished(new TaskResult<ProjectWrapper>(ex));
                            }
                        }
                    }
                }
            }
        };
        return receiver;
    }

The ProjectWrapper _wrapper is just a simple Class that holds data, no logic is done there. The _listener just displays on the callback method a little Toast message. I debugged my app to make shure the download() Method is invoked only once. I hope you can help me find the error.

like image 647
BananaBuster Avatar asked Jul 12 '12 08:07

BananaBuster


People also ask

Where is the download manager on Samsung?

You can find downloads on Android in My Files or File Manager. You can find these apps in the app drawer of your Android device. Within My Files or File Manager, navigate to the Downloads folder to find everything you downloaded.


1 Answers

Unfortunately, DownloadManager is buggy and doesn't work correctly on all devices. Your problem is reported here: https://code.google.com/p/android/issues/detail?id=18462

like image 70
qbait Avatar answered Oct 23 '22 02:10

qbait