android - 我无法在 Android 10 中使用 DownloadManger 下载文件?
问题描述
最近我将我的应用程序更新到Android 10 / API 29
现在,我无法使用 android 默认下载管理器下载图像文件并显示以下错误。
>>>>>: java.lang.SecurityException: Unsupported path /storage/emulated/0/MyFolder/RPS_20200930_191705.png
我的代码
private boolean downloadTask(String url) throws Exception {
if (!url.startsWith("http")) {
return false;
}
String name = new SimpleDateFormat("'RPS_'yyyyMMdd_HHmmss'.png'", Locale.ENGLISH).format(new Date());
try {
File file = new File(Environment.getExternalStorageDirectory(), "MyFolder");
if (!file.exists()) {
//noinspection ResultOfMethodCallIgnored
file.mkdirs();
}
File result = new File(file.getAbsolutePath() + File.separator + name);
DownloadManager downloadManager = (DownloadManager) mContext.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(WebActivity.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;
}
甚至我还添加了读/写权限和 LegacyExternalStorag manifiest.xml
android:requestLegacyExternalStorage="true"
和使用
downloadTask("https://linkpicture.com/Images/gplogo.png");
解决方案
推荐阅读
- networking - 您可以在法定人数中混合服务器品牌吗?
- sql - SQL Server 日期时间说明
- php - 表字段将 VARCHAR 记录为 0
- mysql - 如何在两个表之间插入关系,其中确定的数据集与主键相关?
- swift - 如何打印数组的偶数总和/平均和奇数总和/平均?
- .net - 保护桌面应用程序和 Web API 之间的通信
- bash - 如何在 bash 中使用 getopts 并让它与额外的标志一起使用?
- python - 如果 2 个单词的第一个字母为 True,则返回 True 或 False 的函数
- node.js - Node.js webapp 中的 NPM 错误
- sql - Hadoop SQL 需要一个表名才能获取时间。如何获得表?