CropImage.getActivityResult(data).bitmap = NULL
See original GitHub issueVersion 2.0.3
The Issue CropImage.getActivityResult(data).bitmap = NULL
The Code
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
when (requestCode) {
GALLERY_REQUEST_CODE -> {
data?.data?.let { uri ->
activity?.let {
Log.d(TAG, "UpdateBlogFragment: onActivityResult: $uri")
launchImageCrop(uri)
}
} ?: showImageSelectionError()
}
CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE -> {
val resultBitmap: ByteArray?
Log.d(TAG, "CROP: CROP_IMAGE_ACTIVITY_REQUEST_CODE")
val result = CropImage.getActivityResult(data)
if (result == null) {
Log.d(TAG, "UpdateBlogFragment: onActivityResult: result = NULL")
}
val resultUri = result?.uri
Log.d(TAG, "UpdateBlogFragment: onActivityResult resultUri: ${resultUri.toString()}")
resultBitmap = result?.bitmap?.toByteArray()
Log.d(TAG, "UpdateBlogFragment: onActivityResult resultBitmap Size: ${resultBitmap?.size}")
val resultImage: String = Base64.decode(resultBitmap, Base64.DEFAULT).decodeToString()
viewModel.setUpdatedUri(resultImage)
}
CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE -> {
Log.d(TAG, "CROP: ERROR")
showImageSelectionError()
}
}
}
}
// extension function to convert bitmap to byte array
private fun Bitmap.toByteArray(): ByteArray {
ByteArrayOutputStream().apply {
compress(Bitmap.CompressFormat.JPEG, 10, this)
return toByteArray()
}
}
// extension function to convert byte array to bitmap
private fun ByteArray.toBitmap(): Bitmap {
return BitmapFactory.decodeByteArray(this, 0, size)
}
Expected behavior I store my Images for the application in the backend and cashe as base64 strings (Works nice!) I need the result to be in ByteArray not URI (I actually dont know how to extract a ByteArray from the URI) So I try and extract the bitmap with .bitmap from th URI and then convert it to a ByteArray with the Extention function I wrote below My viewModel also expects a base64 string or ByteArray
What happens D/AppDebug: CROP: CROP_IMAGE_ACTIVITY_REQUEST_CODE UpdateBlogFragment: onActivityResult resultUri: content://za.co.zone.cupio.cropper.fileprovider/my_images/Pictures/cropped7411884326467077015.jpg UpdateBlogFragment: onActivityResult resultBitmap Size: null D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main … Caused by: java.lang.NullPointerException: Attempt to get length of null array at android.util.Base64.decode(Base64.java:138) at za.co.zone.cupio.ui.main.blog.UpdateBlogFragment.onActivityResult(UpdateBlogFragment.kt:132)
Code Involved
val resultImage: String = Base64.decode(resultBitmap, Base64.DEFAULT).decodeToString()
Smartphone AVD Pixel 4 XL API 30 64
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (13 by maintainers)
Top GitHub Comments
I found this while re-factoring old working code from passing the URi to passing a base64 String for the image. The Above is when I edit a existing picture, however I did not get to the part where I create a new blog and save a new picture . That part is actually working as intended and I have though of a work around - but it will not solve this issue. It will however finish my project - I will keep this version for testing and work on the work around whereby I will grab the picture rendered by Glide from the view and convert that to ByteArray and then to base64 String. If it works on making a new Blog post I will implement it on this Edit blog also.
The BUG then would be that Crop does not build the URI correctly and the extraction to bitmap fails, am I correct?
Afterthought As I think about it the Intent to pass the picture as a base64 String will / or can blow the data parcel size Passing the URi between Activities might be better suited to older API versions. However sharing the picture as a base64 String to Other applications like Telegram/WhatsApp/Gmail with Implicit Intent will be OK
@Morons I was taking a look with more attention today.
We have an issue because a bitmap is a huge data to pass between activities. When you try to get
getOriginalBitmap
we have the documentation:I was trying to improve this, creating a bitmap, but is too huge for the
Parcel
in the result activity.So the solution for the library is to have a new method that will need the context to get the bitmap from the internal uri. Hope this solve you problem. is coming on next release
2.1.1
as soon the tickets are merged