DownloadManager 无法创建目标文件 /E/cutils:|无法创建 mkdirat |无法确保目录 |SDK<21

问题描述

我正在使用以下代码使用 DownloadManager 下载文件。下载适用于 SDK 21+ 的真实设备和模拟器。

但是,在具有 SDK 19 及以下版本的真实设备和模拟器上,下载失败并在 Logcat 中出现以下错误:

三星设备:

E/cutils: Failed to mkdirat(/storage/extSdCard/Android): Read-only file system

W/ContextImpl: Failed to ensure directory: /storage/extSdCard/Android/data/com.example.downloadcatalogues/files

I/DownloadManager: Download 71 finished with status FILE_ERROR

模拟器:

W/DownloadManager: Failed to create target file /storage/sdcard/Android/data/com.example.downloadcatalogues/files/test/123.zip

I/DownloadManager: Download 71 finished with status FILE_ERROR

如果我将位置更改为 Environment.getExternalStorageDirectory(),我会得到相同的行为。

清单

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
        android:usesCleartextTraffic="true"
</application>

活动

findViewById(R.id.extrauma_button).setOnClickListener(v -> {
            String urlZipFile = "https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-large-zip-file.zip";
            downloadPro(urlZipFile,"extrauma");
            });

private void downloadPro(String url,String title){
        DownloadManager downloadManager;

    try{
        File file = new File(getApplicationContext().getExternalFilesDir(null).getAbsolutePath()+"/test/","123.zip");

    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url))
            .setTitle(title)
            .setDescription("Downloading")Notification
            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
            .setDestinationUri(Uri.fromFile(file))
           .setRequiresCharging(false)
            .setAllowedOverMetered(true)
            .setAllowedOverRoaming(true);

    downloadManager = (DownloadManager) getApplicationContext().getSystemService(DOWNLOAD_SERVICE);
    long downloadID = downloadManager.enqueue(request);
    Log.v("downloadProId",downloadID+"");
}
catch (Exception e){
    Log.w("stopped",e.toString());
}
}

注意: 如果我在具有 SDK 19 及以下版本的设备上的 Chrome 浏览器上尝试下载链接,则下载链接有效!

我错过了什么吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)