Android 11 下载文件到下载文件夹不起作用

2024-03-08

目前我正在尝试使用 DownloadManager 下载文件,但这不起作用,下载开始,但下载后下载文件夹内没有文件。

这就是我的代码:

 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;
}

日志显示:下载不正确,状态 [1] 原因 [0]

自 Android 11 以来,除了新的存储规则之外,还有其他变化吗?


我在 Stackoverflow 上找到了解决方案(找不到链接了)

 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;
}

这应该适用于 Android 11

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 11 下载文件到下载文件夹不起作用 的相关文章

随机推荐