尝试在Android sdk 22中使用 DownloadManager 下载文件时出现此错误:
java.lang.IllegalStateException: Unable to create directory: /storage/sdcard/Download
at android.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java:538)
该应用已获得权限WRITE_EXTERNAL_STORAGE
,在检查时,它会返回 true
使用带有sdk版本25
的仿真器时也可以使用它val downloadManager = context.getSystemService(DOWNLOAD_SERVICE) as DownloadManager
val request = DownloadManager.Request(Uri.parse("https://xxxxxx.xx/public/App/xxx.apk"))
request.setDescription(context.getString(R.string.downloading_description)).setTitle(context.getString(R.string.app_name))
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "update.apk")
downloadManager.enqueue(request)
context.registerReceiver(onComplete, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
此代码适用于sdk 25