Search code examples
androidkotlindownloadandroid-download-managerimagedownload

how to download image from url android


please dont mark it as duplicate...i referring to this answer--> https://stackoverflow.com/a/54986577/12553303

nothing is happening on click of button...not getting download

 download.setOnClickListener {
                    if (ActivityCompat.checkSelfPermission(applicationContext,
                            Manifest.permission.READ_EXTERNAL_STORAGE) !== PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(
                            applicationContext,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE) !== PackageManager.PERMISSION_GRANTED
                    ) {

                        // this will request for permission when user has not granted permission for the app
                        ActivityCompat.requestPermissions(this@Product_details,
                            arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE,
                                Manifest.permission.WRITE_EXTERNAL_STORAGE),
                            1)
                    } else {
                        //Download Script
                        val downloadManager: DownloadManager = getSystemService(DOWNLOAD_SERVICE) as DownloadManager
                        val uri = Uri.parse(product_images)
                        val request: DownloadManager.Request = DownloadManager.Request(uri)
                        request.setVisibleInDownloadsUi(true)
                        Toast.makeText(
                            applicationContext,
                           uri.toString(),
                            Toast.LENGTH_LONG
                        ).show()
                        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
                        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                            uri.lastPathSegment)
                        downloadManager.enqueue(request)
                    }
                }

where product_image ==>val product_images:String =intent1.getStringExtra("image")

need help thanks


Solution

  • Do away with that permission code.

    Just start the downloadmanager .

    For which you do not need any permission.