Android DownloadManager “Impossible to open file”

后端 未结 2 519
陌清茗
陌清茗 2021-02-09 22:43

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 erro

相关标签:
2条回答
  • 2021-02-09 23:31

    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);
            }
    
    0 讨论(0)
  • 2021-02-09 23:37

    Okay I found the problem !

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

    public DownloadManager.Request setMimeType (String mimeType);
    
    0 讨论(0)
提交回复
热议问题