Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android DownloadManager "Impossible to open file"

I'm using DownloadManager to download a file from a webService. The download end successfuly, but when I try to open the new file in the "download" folder I've got this error "Impossible to open file" (and I know I can open this type of file).

Also, when I plug my phone to my computer and when I open the download file with it, the file open successfuly and is not corrupted.

I don't have other error, so I'm really lost !

Here my code:

    /*Data*/
    int filePosition = position - _subFolderNameList.length;
    String url = _folder.getFiles().get(filePosition).getUrl();
    String FileName = _folder.getFiles().get(filePosition).getName();
    String Description = _folder.getFiles().get(filePosition).getUrl();

    /*Prepare request*/
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
    request.setDescription(Description);
    request.setTitle(FileName);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    }
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, FileName);

    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    manager.enqueue(request); // Send request

Edit: Permission in the Manifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
like image 953
Maugun Avatar asked Mar 21 '13 14:03

Maugun


People also ask

Does Android need download manager?

If you're usually on an erratic mobile network, the chances of this happening are even higher. Hence, you need a download manager. Download managers can help you overcome several common hassles about downloading from the internet.

How do I use download manager on Android?

Once you are sure DownloadManager is available, you can do something like this: String url = "url you want to download"; DownloadManager. Request request = new DownloadManager. Request(Uri.

What does download manager do Android?

The download manager is a system service that handles long-running HTTP downloads. Clients may request that a URI be downloaded to a particular destination file.


2 Answers

Okay I found the problem !

I had to specify the "MIME content type" of the file using setMimeType().

public DownloadManager.Request setMimeType (String mimeType);
like image 54
Maugun Avatar answered Sep 30 '22 15:09

Maugun


Yes you have to specify the MIME type. Adding a few more details to the accepted answer.

public DownloadManager.Request setMimeType (String mimeType);

To get MIME type you can use the following function.

private String getMimeFromFileName(String fileName) {
    MimeTypeMap map = MimeTypeMap.getSingleton();
    String ext = MimeTypeMap.getFileExtensionFromUrl(fileName);
    return map.getMimeTypeFromExtension(ext);
}

and the following is the Xamarin.Android implementation of the same :

        private static string GetMimeTypeFromFileName(string fileName)
        {
            var map = MimeTypeMap.Singleton;
            var ext = MimeTypeMap.GetFileExtensionFromUrl(fileName);
            return map.GetMimeTypeFromExtension(ext);
        }
like image 39
Rohit Vipin Mathews Avatar answered Sep 30 '22 15:09

Rohit Vipin Mathews