1

I have a list of records which I am getting from the API call, on the each item of that list I have a file name with other data. That file name is something like f354jklja.png or kioad87943kjkjd.pdf. I am using that list on the recyclerview, in the each recyclerview item I have a icon , what I want to do is on the click of that icon corresponding file will be download on the phone storage. How can I do that I am not getting any proper idea. Can anyone please help me to solve this?

override fun showPrecription(record: Record) { if (record.file!=null){ val context: Context = this // Check if the app has the WRITE_EXTERNAL_STORAGE permission if (ContextCompat.checkSelfPermission( context, Manifest.permission.WRITE_EXTERNAL_STORAGE ) != PackageManager.PERMISSION_GRANTED ) { // Request the permission ActivityCompat.requestPermissions( this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION ) fileNameToDownload=record.file } else{ downloadFile(Helper.RECORDS_URL,record.file) } }else Toast.makeText(this,"File path is not exist",Toast.LENGTH_LONG).show() // Get the base context } 

This is the function what will trigger on the click of the recyclerview item:

 fun downloadFile(fileUrl: String, fileName: String) { val request = DownloadManager.Request(Uri.parse(fileUrl)) .setTitle(fileName) .setDescription("Downloading") .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName) val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager downloadManager.enqueue(request) } downloadFile function . 

Using this code the file is not downloading in my phone storage.

4
  • an answer here: stackoverflow.com/a/64059674/413127 Commented Nov 24, 2023 at 8:34
  • Not working , Can you help me a little more Commented Nov 24, 2023 at 9:02
  • do you have the INTERNET permission in your manifest? can't help much without more information. Is there any error in the logs? Commented Nov 24, 2023 at 9:08
  • Yes I have given the internet permission , there is no error logs but the problem is on the click of that icon file is not downloding nothing happening Commented Nov 24, 2023 at 9:12

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.