Skip to main content
removed deprecated tag
Source Link

I have a list of records which I am getting from the ApiAPI 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 recyclerviewrecyclerview, in the each recyclerviewrecyclerview item I have a icon , what I want to do is on the click of that icon corospondingcorresponding file will be download on the phone storage. How can iI do that I am not getting any proper idea. AnyoneCan anyone please help me to solve this.?

thisThis is the function what will trigertrigger on the click of the recyclerviewrecyclerview 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.   

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

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 corosponding file will be download on the phone storage. How can i do that I am not getting any proper idea. Anyone please help me to solve this.

this is the function what will triger 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.   

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?

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.

Source Link

How to download a file using file name

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 corosponding file will be download on the phone storage. How can i do that I am not getting any proper idea. 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 triger 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.