首页 > 解决方案 > bitmap.sameAs() 不适用于 Intent Picking 的图像比较

问题描述

我的应用程序检查新选择的图像是否与以前的图像相同,这样我就不需要更新云数据,但newBitmap.sameAs(oldBitmap)即使我选择了同一张照片,也总是返回 false。

我的一段代码:

private lateinit var oldBitmap: Bitmap
private lateinit var newBitmap: Bitmap

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_my_profile)

    myProfilePictureUrl = intent.getStringExtra("myProfilePictureUrl")!!

    if (myProfilePictureUrl.isNotEmpty()) {
        Picasso.get().load(myProfilePictureUrl).into(my_profile_imageView)
    }

    oldBitmap = (my_profile_imageView.drawable as BitmapDrawable).bitmap       //for later comparison

    my_profile_imageView.setOnClickListener {
        val intent = Intent(Intent.ACTION_PICK)

        intent.type = "image/*"

        startActivityForResult(intent, RESULT_CODE_PICK_IMAGE)
    }
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
   super.onActivityResult(requestCode, resultCode, data)

   if (requestCode == RESULT_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) {
       val inputStream = contentResolver.openInputStream(data!!.data!!)

       val bitmap = BitmapFactory.decodeStream(inputStream)

       newBitmap = bitmap              //for comparison

       my_profile_imageView.setImageBitmap(bitmap)
   }
} 

当用户点击操作栏上的“保存”按钮时,它会首先检查它们是否是同一张照片:

private fun updateProfile(nickname: String) {
    if (this::newBitmap.isInitialized && !newBitmap.sameAs(oldBitmap)) {    //here
        
        //upload task code

    } 
}

问题是: !newBitmap.sameAs(oldBitmap)即使它们是同一张照片,总是返回 false,可能是PICASSO的问题?或者我以前比较的方式不对。任何帮助,将不胜感激。

标签: androidkotlinbitmappicassoandroid-bitmap

解决方案


推荐阅读