Android DownloadManager "Impossible to open file"
Asked Answered
S

2

7

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" />
Sternson answered 21/3, 2013 at 14:5 Comment(4)
what type of file are you downloading? and how are you trying to "open" it? You've only posted code for downloading.Combo
I try to downloading all kind of extension but for the test I only try with file like PDF because I know I can open it with my phone. To open it I just go in the download folder (with my phone) and I just click on the file.Sternson
Have you ensured you can open .pdf files, downloaded by hand with your phone (Maybe theres just no app installed to handle the VIEW Intent)? Because I couldnt open many file types with my device by default...Cytologist
Yes I am, because I trie to download the same files with others application (e.g google chrome), and when I open it in the download folder that works. Also I can't open images (JPEG, PNG etc.) download by my app too. I guess it's a reading permission problem or that the file isn't recognized by his extension but it's weird.Sternson
S
5

Okay I found the problem !

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

public DownloadManager.Request setMimeType (String mimeType);
Sternson answered 27/3, 2013 at 10:28 Comment(1)
Hi, not sure if you still around SOF, but if you are downloading from a link to file which you don't know what file extension is that, how do you know which MIME type to set?Overstock
C
3

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);
        }
Clouded answered 8/2, 2016 at 15:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.