Skip to content Skip to sidebar Skip to footer

Image Crop And Resize In Android

I followed the instructions below to crop an image. http://coderzheaven.com/index.php/2011/03/crop-an-image-in-android/ The height and width of the final cropped bitmap image are b

Solution 1:

You can used crop image using following code that can solve your problem.

Matrix matrix = new Matrix();
matrix.postScale(0.5f, 0.5f);
Bitmap croppedBitmap = Bitmap.createBitmap(bitmapOriginal, 100, 100,100, 100, matrix, true);

Above method do postScalling of image before cropping, so you can get best result with cropped image without getting OOM error.

For more detail you can refer this blog


Solution 2:

My co-worker just hipped me to this Android API... see ThumbnailUtils.

It does resizing and cropping for you.

http://developer.android.com/reference/android/media/ThumbnailUtils.html#extractThumbnail(android.graphics.Bitmap, int, int)


Solution 3:

If your code varies from the example, you will have to post it to get precise help. Otherwise, based only on the log - here is an answer:

In the example you are using, the developer is cropping an image in his app's resources folder (which could be relatively small to start) to a 300x300 bitmap image. First, try cropping a smaller size image in your own app, to a smaller output size, to see if there is a problem with the code, or if you are using an image that is too large for your code or device.

Since you are saying the final image you are cropping is larger that the screen, that is probably the issue, and you are encountering a basic challenge of using bitmaps in Android. Bitmap file size is a function of pixel width times height - they get bigger fast.

You can solve by googling other parts of your log, like "bitmap size exceeds VM budget" and you will find several threads on stackoverflow that will help, like this one.

Strange out of memory issue while loading an image to a Bitmap object

You may solve it by using bitmap options and inSampleSize, and other techniques.


Solution 4:

Look at the similar discussion here, they seem to be having the same byte external allocation too large for this process error.


Solution 5:

I further verified the following instructions.

http://coderzheaven.com/index.php/2011/03/crop-an-image-in-android/

I found that the codes are not good enough and some of them are redundant.

To crop a big bitmap image, there is no need to use matrix to do the task.

Just use canvas.drawBitmap method can crop the big image !

Also no need to touch the BitmapFactory.Options.

Now I simply crop the image and let the imageview resize it. No more out-of-memory error. Bingo !


Post a Comment for "Image Crop And Resize In Android"