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
Android Multiple Selection Listview & Textview
Picasso Illegalargumentexception Target Must Not Be Null
How to Use Startactivityforresult() for Email Intent
Show Toast Widget Underneath a View
Get Gps Location in a Broadcast Receiver/Or Service to Broadcast Receiver Data Transfer
Android - Add Textview to Layout When Button Is Pressed
Default Activity Not Found in Android Studio
Equivalent of Clean & Build in Android Studio
Is My App or Its Dependencies Violating the Android Advertising Id Policy
Android Screen Size Hdpi, Ldpi, Mdpi
Android: How to Programmatically Set the Size of a Layout
Does Setwidth(Int Pixels) Use Dip or Px
Gridview VS Gridlayout in Android Apps