question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CropImage.getActivityResult(data).bitmap = NULL

See original GitHub issue

Version 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:closed
  • Created 3 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
Moronscommented, Feb 7, 2021

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

0reactions
Canatocommented, Feb 26, 2021

@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:

Null if uri used to load image or activity result is used.

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Always Null returned after cropping a photo from a Uri in ...
I tried Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData()); when current version is equal or greater than Lollipop, ...
Read more >
com.theartofdev.edmodo.cropper.CropImage# ...
getActivityResult (data); if (resultCode == RESULT_OK) { mImageUri = result. ... getParcelableExtra("icon"); if (bitmap == null) { Uri uri = data.
Read more >
CropImage.getActivityResult - Java
Get CropImageActivity result data object for crop image activity started using #activity(Uri). Popular methods of CropImage. activity. Create ActivityBuilder ...
Read more >
Help with image cropping - MSDN - Microsoft
However after the image is crop it return null on Bitmap bitmap ... Ok) { CropImage(); } else if (requestCode == 2) {...
Read more >
Always Null returned after cropping a photo from a Uri in ...
But, on Android Lollipop, I always got null from data. ... to store the big bitmap public static void cropImage(Uri uri, Activity activity,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found