1. Are you ready for the Galaxy S20? Here is everything we know so far!

Help enabling downloads through webview (Kotlin)

Discussion in 'Android Development' started by Maximiliano Corbo, Apr 7, 2021.

  1. Maximiliano Corbo

    Thread Starter

    Good day. I'm new to android development and I'm trying to develop a simple webview application, picked a nice template and went through the steps and made good progress, I managed to load my site fully and enable javascript, that works as intended, however I'm not able to make the app download anything, I host a few pdf files that should open or download through it, but nothing happens.

    I looked at a few answers here and it is to my understanding that I need to specifically add a function for that, could you give me a hand? I have tried multiple different code and tweaking them, but I wasn't able to get it to work, here is my base code:
    Code (Text):
    1. package com.logista.test.ui.home
    2.  
    3. import android.os.Bundle
    4. import android.view.LayoutInflater
    5. import android.view.View
    6. import android.view.ViewGroup
    7. import android.webkit.WebResourceRequest
    8. import android.webkit.WebView
    9. import android.webkit.WebViewClient
    10. import android.app.DownloadManager
    11. import androidx.fragment.app.Fragment
    12. import androidx.lifecycle.ViewModelProvider
    13. import com.logista.test.R
    14.  
    15.  
    16. class HomeFragment : Fragment() {
    17.  
    18.     private lateinit var homeViewModel: HomeViewModel
    19.  
    20.     override fun onCreateView(
    21.             inflater: LayoutInflater,
    22.             container: ViewGroup?,
    23.             savedInstanceState: Bundle?
    24.     ): View? {
    25.         homeViewModel =
    26.                 ViewModelProvider(this).get(HomeViewModel::class.java)
    27.         val root = inflater.inflate(R.layout.fragment_home, container, false)
    28.         val myWebView: WebView = root.findViewById(R.id.webview)
    29.         myWebView.webViewClient = WebViewClient()
    30.         myWebView.settings.javaScriptEnabled = true
    31.         myWebView.loadUrl("https://www.example.org/")
    32.         return root
    33.     }
    34. }
    I believe I should be adding
    Code (Text):
    1. import android.app.DownloadManager
    And tweak this
    Code (Text):
    1.         // Set web view download listener
    2.  
    3.         web_view.setDownloadListener(DownloadListener {
    4.             url,
    5.             userAgent,
    6.             contentDescription,
    7.             mimetype,
    8.             contentLength ->
    9.  
    10.             // Initialize download request
    11.             val request = DownloadManager.Request(Uri.parse(url))
    12.  
    13.             // Get the cookie
    14.             val cookies = CookieManager.getInstance().getCookie(url)
    15.  
    16.             // Add the download request header
    17.             request.addRequestHeader("Cookie",cookies)
    18.             request.addRequestHeader("User-Agent",userAgent)
    19.  
    20.             // Set download request description
    21.             request.setDescription("Downloading requested file....")
    22.  
    23.             // Set download request mime tytpe
    24.             request.setMimeType(mimetype)
    25.  
    26.             // Allow scanning
    27.             request.allowScanningByMediaScanner()
    28.  
    29.             // Download request notification setting
    30.             request.setNotificationVisibility(
    31.                     DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
    32.  
    33.             // Guess the file name
    34.             val fileName = URLUtil.guessFileName(url, contentDescription, mimetype)
    35.  
    36.             // Set a destination storage for downloaded file
    37.             request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)
    38.  
    39.             // Set request title
    40.             request.setTitle(URLUtil.guessFileName(url, contentDescription, mimetype));
    41.  
    42.  
    43.             // DownloadManager request more settings
    44.             request.setAllowedOverMetered(true)
    45.             request.setAllowedOverRoaming(false)
    46.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    47.                 request.setRequiresCharging(false)
    48.                 request.setRequiresDeviceIdle(false)
    49.             }
    50.             request.setVisibleInDownloadsUi(true)
    51.  
    52.  
    53.             // Get the system download service
    54.             val dManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
    55.  
    56.             // Finally, request the download to system download service
    57.             dManager.enqueue(request)
    58.         })
    59.  
    60.  
    61.         // Load button click listener
    62.         button_load.setOnClickListener{
    63.             // Load url in a web view
    64.             web_view.loadUrl(url)
    65.         }
    66.  
    67.     }
    Taken from here: https://android--code.blogspot.com/2018/03/android-kotlin-webview-file-download.html

    I did the basics, renaming the function accordingly and such, but it gives me quite a few errors when building the app, for instance uri isn't defined, cookiemanager isn't defined, environment isn't defined, build isn't defined, and such, could you give me some guidance?
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - Help enabling downloads
  1. Frizzallen
    Replies:
    2
    Views:
    209
  2. 1987jjc
    Replies:
    1
    Views:
    296
  3. mahinder
    Replies:
    3
    Views:
    257
  4. jumper_d
    Replies:
    3
    Views:
    280
  5. Nikolai Savulkin
    Replies:
    0
    Views:
    221
  6. samiy
    Replies:
    1
    Views:
    190
  7. gremlinkurst
    Replies:
    8
    Views:
    368
  8. scarol1296
    Replies:
    1
    Views:
    227
  9. Stened91
    Replies:
    3
    Views:
    377
  10. NirupaR
    Replies:
    0
    Views:
    185

Share This Page

Loading...