Android: Rotate Image Without Loading It to Memory

Android: rotate image without loading it to memory

you should decode decode the images using Bitmap. you should follow Loading Large Image presented by google on how to do it.. it helped me alot, you'll notice the large difference in the RAM usage..

UPDATE if all you want is just rotate the image you can use this code

Matrix matrix = new Matrix();
matrix.setRotate(90);
result = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight(), matrix, false);

if you just need to set the image orientation (for example the photo orientation when it's was taken) you can use

ExifInterface exif = new ExifInterface(filePath);

with the attribute ExifInterface.TAG_ORIENTATION
I hope this helps you

Android - Rotate Image avoiding OutOfMemory

The short answer is, Yes, this code may cause OutOfMemory. I don't think that there is a simpler solution than increasing app heap size. I believe that @CommonsWare is right, and often OutOfMemory is an indication of wrong programming. But there are some situations when you need, ehm, huge memory. Rotation of a huge image is definitely one of such situations.

You can use native code (NDK) instead of asking for increased heap size, but this is definitely not easier. And it will still needs lots of memory, so there is no advantage in going for C++ (except that it works on 2.3).

Rotating bitmap causes outOfMemoryException

I have suggestions for you.

1) When you have any memory hunger task, use in methods and if possible with AsyncTask.

2) Declare objects as WeakReference. This will give you chance to release memory after use. See below example.

public class RotateTask extends AsyncTask<Void, Void, Bitmap> {
private WeakReference<ImageView> imgInputView;
private WeakReference<Bitmap> rotateBitmap;

public RotateTask(ImageView imgInputView){
this.imgInputView = new WeakReference<ImageView>(imgInputView);
}

@Override
protected void onPreExecute() {
//if you want to show progress dialog
}

@Override
protected Bitmap doInBackground(Void... params) {
Matrix matrix = new Matrix();
matrix.postRotate(90);
rotateBitmap = new WeakReference<Bitmap>(Bitmap.createBitmap(rotated, 0, 0,rotated.getWidth(), rotated.getHeight(), matrix, true));
return rotateBitmap.get();
}

@Override
protected void onPostExecute(Bitmap result) {
//dismiss progress dialog
imgInputView.get().setImageBitmap(result);
}
}

This task has all the views and object as WeakReference. When this task is completed, all the memory used by this Task is free. Try this approach. I used in my application.

Android Rotate image/photo

I think your problem is arising because you are setting inSampleSize to 4. This means the returned image will be a factor of 4 smaller than the original image.

http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize

Try setting options.inSampleSize to 1 - does this help?

Be careful when dealing with images though - you have very little memory to play with in an Android app. Loading just a couple of large images into memory at once can often cause your app to crash.



Related Topics



Leave a reply



Submit