【问题标题】:Convert Image from RecyclerView to Bitmap将图像从 RecyclerView 转换为位图
【发布时间】:2018-08-31 14:05:15
【问题描述】:

我需要从我的 recyclerview 中将已点击的特定图像发送到将显示该图像的新活动。问题是如何将我的图像转换为位图或类似的东西,以便我可以使用意图发送它们? 这是我的代码:

RecyclerViewAdapter.kt

class ViewHolder(view: View) : RecyclerView.ViewHolder(view){

    fun bindItem(items: Item) {
        itemView.name.text = items.name
        itemView.desc.text = items.desc
        Glide.with(itemView.context).load(items.image).into(itemView.image)

        itemView.setOnClickListener {
            itemView.context.startActivity(itemView.context.intentFor<DetailsActivity>("image" to bitmap, "name" to items.name, "desc" to items.desc))
        }
    }
}

第二活动

val intent = intent
    name = intent.getStringExtra("name")
    image = intent.getStringExtra("image")
    desc = intent.getStringExtra("desc")

    nameTextView.text = name
    descTextView.text = desc
    Glide.with(this)
            .load(image)
            .into(imgPhotos)

我已经尝试了该站点上提供的所有代码,但它们都不起作用。谢谢!

【问题讨论】:

  • 在您的 SecondActivity 中,您是否尝试过使用 getStringExtra("image") 而不是 getParcelableExtra("image")
  • 我应该如何在第二个活动中声明图像变量?
  • 为什么不将图像 url 发送到您的活动,然后从 url 加载它
  • 我的图片在我的可绘制文件夹中
  • 然后发送资源ID

标签: android android-intent android-recyclerview kotlin


【解决方案1】:

我没有使用 Glide 但应该是这样的。

 lateinit imageBitmap : Bitmap

 Glide.with(this).asBitmap().load("YOUR_URL").into(object: Target<Bitmap>{
      override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
          imageBitmap = resource
      }
 })

Bitmap可以直接发送,因为它是Parcelable

intent.putExtra("image",imageBitmap)

获取位图

val bitmap  = intent.extras.getParcelable<Bitmap>("image")

【讨论】:

    【解决方案2】:

    将图像转换为 byteArray 并通过 Intent 传递。通过对端intent提取数据,将byteArray转化为bitmap。

    【讨论】:

    • 是的,我已经使用本网站中提供的所有代码做到了这一点,但这不起作用!
    • 你能告诉我是哪篇文章吗?我只想看代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    • 2012-10-24
    • 1970-01-01
    相关资源
    最近更新 更多