這是我在 android 中從 webView 下載任何內容時嘗試執行相同操作的代碼。
webview.setDownloadListener { url, userAgent, contentDisposition, mimetype, l ->
val request = DownloadManager.Request(Uri.parse(url))
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype))
request.setDescription("Downloading file...")
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
var newuri=FileProvider.getUriForFile(context, "com.shyptsolution.classproject" ".provider", File(context.filesDir,"fileName"))
// request.setDestinationUri(newuri)
request.setDestinationInExternalPublicDir(
context.filesDir.absolutePath,
URLUtil.guessFileName(url, contentDisposition, mimetype)
)
val dm =context.applicationContext.getSystemService(DOWNLOAD_SERVICE) as DownloadManager
dm!!.enqueue(request)
}
但是我收到錯誤,它不是標準目錄。如何將檔案保存在快取或檔案目錄中?
我只想files在下圖中顯示的檔案夾中查看我的檔案。

uj5u.com熱心網友回復:
DownloadManager無法下載到內部存盤,因為它無法訪問該位置。
DownloadManager由于范圍存盤限制,在現代版本的 Android 中非常有限。我建議您使用其他東西,例如 OkHttp,將您的應用程式中的內容下載到您控制的檔案中,包括到內部存盤中。
uj5u.com熱心網友回復:
request.setDestinationInExternalPublicDir(
替換為:
request.setDestinationInExternalFilesDir(
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/436905.html
標籤:爪哇 安卓 科特林 android-webview 安卓下载管理器
