Android DownloadManager "Unable to open file"

I use DownloadManager to download a file from webService. The download success succeeds, but when I try to open a new file in the โ€œdownloadโ€ folder, I have this error โ€œCannot open fileโ€ (and I know that I can open this file type).

In addition, when I connect my phone to my computer, and when I open the file to download with it, the file opens successfully and is not damaged.

I have no other mistake, so I'm really lost!

Here is 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: Resolution in 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" />
+5
source share
2 answers

Ok, I found a problem!

" MIME" , setMimeType().

public DownloadManager.Request setMimeType (String mimeType);
+4

, MIME. .

public DownloadManager.Request setMimeType (String mimeType);

MIME, .

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

Xamarin.Android:

        private static string GetMimeTypeFromFileName(string fileName)
        {
            var map = MimeTypeMap.Singleton;
            var ext = MimeTypeMap.GetFileExtensionFromUrl(fileName);
            return map.GetMimeTypeFromExtension(ext);
        }
+2

All Articles