【问题标题】:How can download image from retrofit response kotlin android如何从改造响应 kotlin android 下载图像
【发布时间】:2022-01-08 21:17:56
【问题描述】:

如何从改造响应 Kotlin 下载图像

我收到了这样的回复

 "image_thumb": "http%3A%2F%2Fdemo.co%2Fflightrecord%2Fimages%2Fpassport%2Fthumb%2Fpassport-307199094910Koala.jpg"

如何使用 FileOutputStream 将图像保存在设备目录中

【问题讨论】:

  • 我认为您需要先解码您的 URL,如下所示 String decodedURL = URLDecoder.decode("URL", "UTF-8");
  • 是的,你是对的,我需要先解码 url

标签: android file-io retrofit


【解决方案1】:

您的解决方案

 if (response.body()?.data != null) {
                        val result = URLDecoder.decode(response.body()?.data?.get(0)?.dof!!, StandardCharsets.UTF_8.name())
                        val image1 = URLDecoder.decode(response.body()?.data?.get(0)?.image_thumb!!, StandardCharsets.UTF_8.name())
                        var imagePath: String

                        Glide.with(this)
                            .asBitmap()
                            .load(image1)
                            .into(object : CustomTarget<Bitmap>(){
                                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                                    val fos: FileOutputStream? =
                                        activity?.openFileOutput( response.body()?.data?.get(0)?.userid!!, AppCompatActivity.MODE_PRIVATE)
                                    resource.compress(Bitmap.CompressFormat.JPEG, 50, fos)
                                    fos?.close()
                                    imagePath = activity?.applicationContext?.getFileStreamPath(response.body()?.data?.get(0)?.userid!!)?.absolutePath?.toUri()
                                        .toString()
                                    handler.passportInsert(
                                        PassportModelClass(
                                            response.body()?.data?.get(0)?.userid!!,
                                            response.body()?.data?.get(0)?.fistname!!,
                                            response.body()?.data?.get(0)?.surname!!,
                                            response.body()?.data?.get(0)?.nationality!!,
                                            result,
                                            response.body()?.data?.get(0)?.gender!!,
                                            imagePath,
                                            response.body()?.data?.get(0)?.age!!.toInt()
                                        )
                                    )
                                    binding.progress.isVisible = false
                                    Toast.makeText(context, "restore successfully", Toast.LENGTH_SHORT).show()
                                }
                                override fun onLoadCleared(placeholder: Drawable?) {
                                }
                            })
                    }

【讨论】:

    猜你喜欢
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 2020-02-22
    • 2017-06-19
    相关资源
    最近更新 更多