3

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.

Ciprian
  • 2,879
  • 3
  • 28
  • 28

1 Answers1

0

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.

azertiti
  • 3,150
  • 17
  • 19
  • Hi, and thanks for your answer. I already have a BroadcastReceiver, the problem is that somehow the mime type is not added, even I've added this: downloadReq.setMimeType(attachment.mimeType); – Ciprian Mar 08 '13 at 11:27
  • I checked the code in my app and no mime was provided. The downloaded file is an .apk and works fine, no issues reported so far. The only difference is that I also specify a filename for the download while you use "". – azertiti Mar 08 '13 at 12:09
  • ok, I also tried to pass the file name in this method call: downloadReq.setDestinationInExternalFilesDir(service, Environment.DIRECTORY_DOWNLOADS, ""); and indeed in the notification center I saw the file name, instead of "- some_value" but the opening still not working, it just shows up a toast saying "Could not open file", even if it is a .png file, I'm trying to figure it out what's different in S2 than the other phones I've tested on... – Ciprian Mar 08 '13 at 12:18