My android WebView apps: kotlin have bug where starting download but at the end it failed

The situation: my website generate pdf form html using html2pdf JavaScript and try below solution in
It successfully start downloading without crashing but the pdf file downloads is unsuccessful.

the kotlin code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>private fun download(
url: String,
userAgent: String,
contentDisposition: String,
mimeType: String,
contentLength: Long
) {
val folder = File(Environment.getExternalStorageDirectory().toString() + "/Download/Image")
if (!folder.exists()) {
folder.mkdirs()
}
Toast.makeText(this, "Download Started", Toast.LENGTH_SHORT).show()
// Remove the 'blob:' prefix if it exists
val cleanUrl = url.replace("blob:", "")
val request = DownloadManager.Request(Uri.parse(cleanUrl))
request.setMimeType(mimeType)
val cookie = CookieManager.getInstance().getCookie(cleanUrl)
request.addRequestHeader("cookie", cookie)
request.addRequestHeader("User-Agent", userAgent)
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE
)
val fileName = URLUtil.guessFileName(cleanUrl, contentDisposition, mimeType)
request.setTitle(fileName)
request.setNotificationVisibility(
DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED
)
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
"Image/$fileName"
)
val downloadManager = getSystemService(DOWNLOAD_SERVICE) as DownloadManager
downloadManager.enqueue(request)
}
</code>
<code>private fun download( url: String, userAgent: String, contentDisposition: String, mimeType: String, contentLength: Long ) { val folder = File(Environment.getExternalStorageDirectory().toString() + "/Download/Image") if (!folder.exists()) { folder.mkdirs() } Toast.makeText(this, "Download Started", Toast.LENGTH_SHORT).show() // Remove the 'blob:' prefix if it exists val cleanUrl = url.replace("blob:", "") val request = DownloadManager.Request(Uri.parse(cleanUrl)) request.setMimeType(mimeType) val cookie = CookieManager.getInstance().getCookie(cleanUrl) request.addRequestHeader("cookie", cookie) request.addRequestHeader("User-Agent", userAgent) request.setAllowedNetworkTypes( DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE ) val fileName = URLUtil.guessFileName(cleanUrl, contentDisposition, mimeType) request.setTitle(fileName) request.setNotificationVisibility( DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED ) request.setDestinationInExternalPublicDir( Environment.DIRECTORY_DOWNLOADS, "Image/$fileName" ) val downloadManager = getSystemService(DOWNLOAD_SERVICE) as DownloadManager downloadManager.enqueue(request) } </code>
private fun download(
    url: String,
    userAgent: String,
    contentDisposition: String,
    mimeType: String,
    contentLength: Long
) {
    val folder = File(Environment.getExternalStorageDirectory().toString() + "/Download/Image")
    if (!folder.exists()) {
        folder.mkdirs()
    }
    Toast.makeText(this, "Download Started", Toast.LENGTH_SHORT).show()

    // Remove the 'blob:' prefix if it exists
    val cleanUrl = url.replace("blob:", "")

    val request = DownloadManager.Request(Uri.parse(cleanUrl))
    request.setMimeType(mimeType)
    val cookie = CookieManager.getInstance().getCookie(cleanUrl)
    request.addRequestHeader("cookie", cookie)
    request.addRequestHeader("User-Agent", userAgent)
    request.setAllowedNetworkTypes(
        DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE
    )
    val fileName = URLUtil.guessFileName(cleanUrl, contentDisposition, mimeType)
    request.setTitle(fileName)
    request.setNotificationVisibility(
        DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED
    )
    request.setDestinationInExternalPublicDir(
        Environment.DIRECTORY_DOWNLOADS,
        "Image/$fileName"
    )
    val downloadManager = getSystemService(DOWNLOAD_SERVICE) as DownloadManager
    downloadManager.enqueue(request)
}

I tried the recommendation about try in real device however the result is the same. Please help 🙂

the logcat:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>2024-06-03 23:13:39.866 7633-7652 EGL_emulation com.coding.meet.webviewtoapp D app_time_stats: avg=5244564.50ms min=5244564.50ms max=5244564.50ms count=1
2024-06-03 23:13:40.542 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 0ms Starting document clone", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:40.883 7633-7652 EGL_emulation com.coding.meet.webviewtoapp D app_time_stats: avg=58.57ms min=8.43ms max=488.87ms count=16
2024-06-03 23:13:40.948 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd6a 0ms Starting document clone", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:41.470 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 931ms Document cloned, using computed rendering", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:41.470 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 931ms Starting DOM parsing", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:41.603 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 1066ms Added image https://smartappx.site/assets/malaysia.png", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:41.644 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 1107ms Added image https://smartappx.site/assets/jps-logo.png", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:42.332 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 1794ms Starting renderer", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:42.337 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 1796ms Canvas renderer initialized (634x1373 at 0,0) with scale 2", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:42.627 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 2074ms Finished rendering", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:43.182 7633-7633 Url com.coding.meet.webviewtoapp D blob:https://smartappx.site/547b98d3-ba77-4c79-a315-35e020c5d34e
2024-06-03 23:13:43.183 7633-7633 userAgent com.coding.meet.webviewtoapp D Mozilla/5.0 (Linux; Android 12; sdk_gphone64_x86_64 Build/SE1A.211212.001.B1; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/91.0.4472.114 Mobile Safari/537.36
2024-06-03 23:13:43.183 7633-7633 contentDisposition com.coding.meet.webviewtoapp D
2024-06-03 23:13:43.183 7633-7633 mimeType com.coding.meet.webviewtoapp D application/pdf
2024-06-03 23:13:43.188 7633-7633 contentLength com.coding.meet.webviewtoapp D 561986
2024-06-03 23:13:43.271 611-2517 Compatibil...geReporter system_server D Compat change id reported: 135634846; UID 10064; state: DISABLED
2024-06-03 23:13:43.272 611-2517 Compatibil...geReporter system_server D Compat change id reported: 177438394; UID 10064; state: DISABLED
2024-06-03 23:13:43.273 611-2517 Compatibil...geReporter system_server D Compat change id reported: 135772972; UID 10064; state: DISABLED
</code>
<code>2024-06-03 23:13:39.866 7633-7652 EGL_emulation com.coding.meet.webviewtoapp D app_time_stats: avg=5244564.50ms min=5244564.50ms max=5244564.50ms count=1 2024-06-03 23:13:40.542 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 0ms Starting document clone", source: https://smartappx.site/js/html2pdf.bundle.min.js (2) 2024-06-03 23:13:40.883 7633-7652 EGL_emulation com.coding.meet.webviewtoapp D app_time_stats: avg=58.57ms min=8.43ms max=488.87ms count=16 2024-06-03 23:13:40.948 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd6a 0ms Starting document clone", source: https://smartappx.site/js/html2pdf.bundle.min.js (2) 2024-06-03 23:13:41.470 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 931ms Document cloned, using computed rendering", source: https://smartappx.site/js/html2pdf.bundle.min.js (2) 2024-06-03 23:13:41.470 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 931ms Starting DOM parsing", source: https://smartappx.site/js/html2pdf.bundle.min.js (2) 2024-06-03 23:13:41.603 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 1066ms Added image https://smartappx.site/assets/malaysia.png", source: https://smartappx.site/js/html2pdf.bundle.min.js (2) 2024-06-03 23:13:41.644 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 1107ms Added image https://smartappx.site/assets/jps-logo.png", source: https://smartappx.site/js/html2pdf.bundle.min.js (2) 2024-06-03 23:13:42.332 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 1794ms Starting renderer", source: https://smartappx.site/js/html2pdf.bundle.min.js (2) 2024-06-03 23:13:42.337 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 1796ms Canvas renderer initialized (634x1373 at 0,0) with scale 2", source: https://smartappx.site/js/html2pdf.bundle.min.js (2) 2024-06-03 23:13:42.627 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 2074ms Finished rendering", source: https://smartappx.site/js/html2pdf.bundle.min.js (2) 2024-06-03 23:13:43.182 7633-7633 Url com.coding.meet.webviewtoapp D blob:https://smartappx.site/547b98d3-ba77-4c79-a315-35e020c5d34e 2024-06-03 23:13:43.183 7633-7633 userAgent com.coding.meet.webviewtoapp D Mozilla/5.0 (Linux; Android 12; sdk_gphone64_x86_64 Build/SE1A.211212.001.B1; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/91.0.4472.114 Mobile Safari/537.36 2024-06-03 23:13:43.183 7633-7633 contentDisposition com.coding.meet.webviewtoapp D 2024-06-03 23:13:43.183 7633-7633 mimeType com.coding.meet.webviewtoapp D application/pdf 2024-06-03 23:13:43.188 7633-7633 contentLength com.coding.meet.webviewtoapp D 561986 2024-06-03 23:13:43.271 611-2517 Compatibil...geReporter system_server D Compat change id reported: 135634846; UID 10064; state: DISABLED 2024-06-03 23:13:43.272 611-2517 Compatibil...geReporter system_server D Compat change id reported: 177438394; UID 10064; state: DISABLED 2024-06-03 23:13:43.273 611-2517 Compatibil...geReporter system_server D Compat change id reported: 135772972; UID 10064; state: DISABLED </code>
2024-06-03 23:13:39.866  7633-7652  EGL_emulation           com.coding.meet.webviewtoapp         D  app_time_stats: avg=5244564.50ms min=5244564.50ms max=5244564.50ms count=1
2024-06-03 23:13:40.542  7633-7633  chromium                com.coding.meet.webviewtoapp         I  [INFO:CONSOLE(2)] "18fdea9cd86 0ms Starting document clone", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:40.883  7633-7652  EGL_emulation           com.coding.meet.webviewtoapp         D  app_time_stats: avg=58.57ms min=8.43ms max=488.87ms count=16
2024-06-03 23:13:40.948  7633-7633  chromium                com.coding.meet.webviewtoapp         I  [INFO:CONSOLE(2)] "18fdea9cd6a 0ms Starting document clone", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:41.470  7633-7633  chromium                com.coding.meet.webviewtoapp         I  [INFO:CONSOLE(2)] "18fdea9cd86 931ms Document cloned, using computed rendering", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:41.470  7633-7633  chromium                com.coding.meet.webviewtoapp         I  [INFO:CONSOLE(2)] "18fdea9cd86 931ms Starting DOM parsing", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:41.603  7633-7633  chromium                com.coding.meet.webviewtoapp         I  [INFO:CONSOLE(2)] "18fdea9cd86 1066ms Added image https://smartappx.site/assets/malaysia.png", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:41.644  7633-7633  chromium                com.coding.meet.webviewtoapp         I  [INFO:CONSOLE(2)] "18fdea9cd86 1107ms Added image https://smartappx.site/assets/jps-logo.png", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:42.332  7633-7633  chromium                com.coding.meet.webviewtoapp         I  [INFO:CONSOLE(2)] "18fdea9cd86 1794ms Starting renderer", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:42.337  7633-7633  chromium                com.coding.meet.webviewtoapp         I  [INFO:CONSOLE(2)] "18fdea9cd86 1796ms Canvas renderer initialized (634x1373 at 0,0) with scale 2", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:42.627  7633-7633  chromium                com.coding.meet.webviewtoapp         I  [INFO:CONSOLE(2)] "18fdea9cd86 2074ms Finished rendering", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:43.182  7633-7633  Url                     com.coding.meet.webviewtoapp         D  blob:https://smartappx.site/547b98d3-ba77-4c79-a315-35e020c5d34e
2024-06-03 23:13:43.183  7633-7633  userAgent               com.coding.meet.webviewtoapp         D  Mozilla/5.0 (Linux; Android 12; sdk_gphone64_x86_64 Build/SE1A.211212.001.B1; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/91.0.4472.114 Mobile Safari/537.36
2024-06-03 23:13:43.183  7633-7633  contentDisposition      com.coding.meet.webviewtoapp         D  
2024-06-03 23:13:43.183  7633-7633  mimeType                com.coding.meet.webviewtoapp         D  application/pdf
2024-06-03 23:13:43.188  7633-7633  contentLength           com.coding.meet.webviewtoapp         D  561986
2024-06-03 23:13:43.271   611-2517  Compatibil...geReporter system_server                        D  Compat change id reported: 135634846; UID 10064; state: DISABLED
2024-06-03 23:13:43.272   611-2517  Compatibil...geReporter system_server                        D  Compat change id reported: 177438394; UID 10064; state: DISABLED
2024-06-03 23:13:43.273   611-2517  Compatibil...geReporter system_server                        D  Compat change id reported: 135772972; UID 10064; state: DISABLED

New contributor

Tengku Muhammad Muzaffar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật