'Android downloading file through DownloadManager not working correctly

I'm trying to download files thru DownloadManager, it works perfectly on most of the phones (Nexus family, S3, etc) but on Galaxy S2 for some reason the download works, but the name of the file is set wrong and when I try to open it (either from notification, either downloads app) it says that the file cannot be opened, even for files like jpeg, gif, png, etc.

enter image description here

Here is the code:

DownloadManager downloadManager = (DownloadManager) service
                .getSystemService(Context.DOWNLOAD_SERVICE);
        DownloadManager.Request downloadReq = new DownloadManager.Request(
                Uri.parse(URL));
        downloadReq
                .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI
                        | DownloadManager.Request.NETWORK_MOBILE);
        downloadReq.allowScanningByMediaScanner();
        downloadReq.setMimeType(attachment.mimeType);
        downloadReq.setTitle(attachment.fileName);
        downloadReq.setDescription("attachment");
        downloadReq.setDestinationInExternalFilesDir(service,
                Environment.DIRECTORY_DOWNLOADS, "");
        downloadReq
                .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE
                        | DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

        downloadIDs.add(downloadManager.enqueue(downloadReq));

Also please note that all the URLs are https, and the phone's android version is 4.1.2 Any idea?

Many Thanks!

Update: if I add the file name in this call:

downloadReq.setDestinationInExternalFilesDir(service,
                Environment.DIRECTORY_DOWNLOADS, attachment.fileName);

the good name is displayed in the notification center.



Solution 1:[1]

You should register yourself to receive a broadcast when the file download is complete. Over there you can also grab the filename. This will need some changes to the code:

Retain the ID returned from enqueue call:

long enqueue = downloadManager.enqueue(downloadReq);

Register a receiver to get the broadcast:

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

declare the receiver:

BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (!DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
            return;
        }
        context.getApplicationContext().unregisterReceiver(receiver);
        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)) {

                String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                Log.i(TAG, "downloaded file " + uriString);                    
            } else {
                Log.i(TAG, "download failed " + c.getInt(columnIndex));                    
            }
        }
    }
};

Assuming a filename for download is not good practice. If you download it again without removing the previous one it will automatically get a suffix.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 azertiti