问题描述
最近我现在将我的应用更新为 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 manifiedt.xml
android:requestLegacyExternalStorage="true"
和用法
downloadTask("https://linkpicture.com/Images/gplogo.png");
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)