My onActivityResult
is not working because getBitmap
is deprecated, any alternative codes to achieve this?
here are the codes that needs to be changed, any suggestions?
val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, selectedPhotoUri)
the getBitmap
is crossed and says its deprecated
This worked for me,
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
val selectedPhotoUri = data.data
try {
selectedPhotoUri?.let {
if(Build.VERSION.SDK_INT < 28) {
val bitmap = MediaStore.Images.Media.getBitmap(
this.contentResolver,
selectedPhotoUri
)
imageView.setImageBitmap(bitmap)
} else {
val source = ImageDecoder.createSource(this.contentResolver, selectedPhotoUri)
val bitmap = ImageDecoder.decodeBitmap(source)
imageView.setImageBitmap(bitmap)
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
This worked well for me in java
ImageDecoder.Source source = ImageDecoder.createSource(this.getContentResolver(), pictureUri);
Bitmap bitmap = ImageDecoder.decodeBitmap(source);
You can use:
private fun getCapturedImage(selectedPhotoUri: Uri): Bitmap {
val bitmap = when {
Build.VERSION.SDK_INT < 28 -> MediaStore.Images.Media.getBitmap(
this.contentResolver,
selectedPhotoUri
)
else -> {
val source = ImageDecoder.createSource(this.contentResolver, selectedPhotoUri)
ImageDecoder.decodeBitmap(source)
}
}
Check the official doc:
This method was deprecated in API level 29. loading of images should be performed through
ImageDecoder#createSource(ContentResolver, Uri)
, which offers modern features like PostProcessor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With