bitmap.sameAs() doesn't work on image comparison from Intent picking

Viewed 192

My application checks whether the new selected image is the same as the previous one so that I don't need to update the cloud data, but newBitmap.sameAs(oldBitmap) always return false even though I pick the same photo.

Piece of my code:

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

And when the user click on the "save" button on the action bar will it first check whether they are the same photo:

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

    } 
}

THE PROBLEM IS: !newBitmap.sameAs(oldBitmap) always return false even they are the same photo, may be the problem of PICASSO? Or the way I used to compare is not right. Any help would be appreciated.

0 Answers
Related