androidandroid-permissionsandroid-download-manager

Android 11 Download File to Download Folder doesn't work


currently I'm trying to download a File with the DownloadManager but that doesn't work, the download starts but there is no File inside the Download Folder after downloading.

Thats my Code:

 private void downloadAddon() {
        try{
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
          //  request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
            request.setTitle("download");
            request.setDescription("apk downloading");
            // request.setAllowedOverRoaming(false);
            request.setDestinationUri(Uri.fromFile(new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) , "mod.mcpack")));
            DownloadManager downloadManager =  (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
            long downloadID = downloadManager.enqueue(request);

            //Just for testing
            if (downloadComplete(downloadID)) {
                Toast.makeText(this, "Download Status: Completed", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this, "Download Status: Error", Toast.LENGTH_SHORT).show();
            }
        }catch (Exception e){
            //Not required, there is no error that crashes the app
            Toast.makeText(this, "Error catched: " + e.getMessage(), Toast.LENGTH_SHORT).show();

        }

}
private boolean downloadComplete(long downloadId){
    DownloadManager dMgr = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    Cursor c= dMgr.query(new DownloadManager.Query().setFilterById(downloadId));

    if(c.moveToFirst()){
        int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));

        if(status == DownloadManager.STATUS_SUCCESSFUL){
            return true; //Download completed, celebrate
        }else{
            int reason = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON));
            Log.d(getPackageName(), "Download not correct, status [" + status + "] reason [" + reason + "]");
            return false;
        }
    }
    return false;
}

The Log says: Download not correct, status [1] reason [0]

Did something changed since Android 11 except the new storage rules?


Solution

  • I found an solution on Stackoverflow (Can't find the link anymore)

     private boolean downloadTask(String url) throws Exception {
        if (!url.startsWith("http")) {
            return false;
        }
        String name = "temp.mcaddon";
        try {
            File file = new File(Environment.getExternalStorageDirectory(), "Download");
            if (!file.exists()) {
                //noinspection ResultOfMethodCallIgnored
                file.mkdirs();
            }
            File result = new File(file.getAbsolutePath() + File.separator + name);
            DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
            request.setDestinationUri(Uri.fromFile(result));
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            if (downloadManager != null) {
                downloadManager.enqueue(request);
            }
            //mToast(mContext, "Starting download...");
            MediaScannerConnection.scanFile(DetailsActivity.this, new String[]{result.toString()}, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        public void onScanCompleted(String path, Uri uri) {
                        }
                    });
        } catch (Exception e) {
            Log.e(">>>>>", e.toString());
            //mToast(this, e.toString());
            return false;
        }
        return true;
    }
    

    This should work for Android 11