如何从 url android 下载图像

how to download image from url android

请不要将其标记为重复...我指的是这个答案-->

点击按钮没有任何反应...没有下载

 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)
                    }
                }

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

需要帮助谢谢

使用 Picasso 或 Glide 库从 URL 下载图像。

试试这个毕加索图书馆

Tthis Link : Picasso

    Picasso.with(MainActivity::this)
       .load(imageUrl)
       .into(imageView, object: com.squareup.picasso.Callback {
                    override fun onSuccess() {
                        //set animations here

                    }

                    override fun onError(e: java.lang.Exception?) {
                        //do smth when there is picture loading error
                    }
                })

删除该权限代码。

只需启动下载管理器。

您不需要任何许可。