通过android中的download Manager进行下载有时无法下载,并且抛出错误路径似乎无效,并且在一段时间后会自行纠正。这是Android Download Manager的非常不正常的行为。
我正在通过request.setDestinationInExternalPublicDir("/app/" , my_app.apk)
下载到外部存储设备
当它不下载时,它会在logcat中抛出此错误:-
W/DownloadManager: Path appears to be invalid: /storage/emulated/0/app/my_app.apk
我认为这是因为我要在Android的自定义目录中下载,但是我不能在其中使用Environment.DIRECTORY_DOWNLOADS
,而只能在自定义目录中下载。
请让我知道某人是否有答案。)
答案 0 :(得分:0)
首先,请确保已创建一个名为“ app”的目录,您要在其中存储apk。 要创建目录:
File path = Environment.getExternalStorageDirectory();
File dir = new File(path.getAbsolutePath(), "/app/");
if (!dir.exists()) {
dir.mkdir();
}
如果您已经创建了这样的目录,请尝试以下操作:
request.setDestinationInExternalPublicDir("/app/" , "my_app.apk");
答案 1 :(得分:0)
您必须确保目录存在,以便可以使用-
File root = new File(context.getExternalFilesDir(null).getAbsolutePath() + "/app/");
if (!root.exists()) {
root.mkdirs();
}
然后您可以使用它来设置下载管理器路径
request.setDestinationUri(Uri.parse("file://" + context.getExternalFilesDir(null).getAbsolutePath() + "/app/my_app.apk"));