I am trying to download and open a PDF file from my WebView. I have tried retrieving the cookie from the WebView, and setting the cookie on a DownloadManager.Request. When my BroadCastReceiver is triggered, the download status states that it has failed.
this.webView?.apply {
settings.domStorageEnabled = true
settings.javaScriptEnabled = true
setDownloadListener { url, _, _, mimetype, _ ->
Log.w("downloading file", url)
val downloadUri = Uri.parse(url)
val downloadRequest = DownloadManager.Request(downloadUri).apply {
setTitle("Title")
setDescription("Downloading file")
Log.w("path", "${downloadUri.lastPathSegment}")
setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, downloadUri.lastPathSegment)
val cookie = getCookie("https://www.example.com", "example_cookie_name")
Log.w("cookie", "${cookie}")
addRequestHeader("Cookie", cookie)
}
val manager: DownloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val downloadId = manager.enqueue(downloadRequest)
registerReceiver(
object: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.w("onReceive", "${intent?.action}")
if (intent !== null && DownloadManager.ACTION_DOWNLOAD_COMPLETE == intent.action) {
val cursor = manager.query(DownloadManager.Query().apply{ setFilterById(downloadId) })
if (cursor.moveToFirst()) {
Log.w("onReceive", "cursor moved")
val downloadStatus = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
val downloadLocalUri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))
val downloadMimeType = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE))
Log.w("onReceive", "$downloadStatus $downloadLocalUri $downloadMimeType")
if (downloadStatus == DownloadManager.STATUS_SUCCESSFUL && downloadLocalUri !== null) {
val viewIntent = Intent(Intent.ACTION_VIEW).apply {
this.setDataAndType(Uri.parse(downloadLocalUri), downloadMimeType)
}
if (viewIntent.resolveActivity(packageManager) !== null) {
startActivity(
Intent.createChooser(viewIntent, "Choose app")
)
} else {
Toast.makeText(
context,
"No app available that can open file",
Toast.LENGTH_SHORT
)
.show()
}
}
}
}
}
},
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
)
}
}
Through the logging in this code, I can confirm that I am getting the cookie out of the WebView.
Is there a better approach to handle this? If not, how do I determine why the download is failing? Also, what am I doing wrong?
Ideally, I would also be able to download the file, and ensure it is deleted after the user has finished viewing the PDF.
CodePudding user response:
You could try something like this before loading the webview's content.
fun setCookies() {
val cookieManager = CookieManager.getInstance()
val cookieString = "your_cookie_here"
cookieManager.setCookie("domain_name_of_the_dowload_url", cookieString)
cookieManager.setAcceptThirdPartyCookies(your_webview_here, true)
}
CodePudding user response:
It turned out that the way I was retrieving the cookie was wrong, it ended up stripping out the key. For future reference, it is quite simple to set the cookie on the download request, with:
addRequestHeader("Cookie", CookieManager.getInstance().getCookie(url))