I wrote a sample application which rotates and scales a bitmap on
clicking a key. When the bitmap is large (1280x1024) I quickly run
into an OutOfMemory exception at CreateBitmap and I need to Force
Close the application. No issues with smaller bitmaps.

I have already tried using bitmap.recycle and setting the bitmap to
null, but the memory does not get freed up. I have seen multiple posts
with similar issues, but could not find one that solves my issue. Do I
need to force the GC at some point? Here is the code I am using:

public class TransformAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout linLayout = new LinearLayout(this);

        mBitmapOrg = BitmapFactory.decodeResource(getResources(),
               R.drawable.sample);

        mScaleWidth = 0.1f;
        mScaleHeight = 0.1f;
        mRotateAngle = 0;

        mImageView = new ImageView(this);

        TransformThePicture();

        // center the Image
        mImageView.setScaleType(ScaleType.CENTER);

        // add ImageView to the Layout
        linLayout.addView(mImageView,
          new LinearLayout.LayoutParams(
                      LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT
                )
        );

        // set LinearLayout as ContentView
        setContentView(linLayout);

    }

    private void TransformThePicture()
    {
        Matrix matrix = new Matrix();
        matrix.postScale(mScaleWidth, mScaleHeight);
        matrix.postRotate(mRotateAngle);

        int width = mBitmapOrg.getWidth();
        int height = mBitmapOrg.getHeight();

        mResizedBitmap = Bitmap.createBitmap(mBitmapOrg, 0, 0,
                width, height, matrix, true);

        mImageView.setImageBitmap(mResizedBitmap);
        mImageView.invalidate();

    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
           if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {

                  mScaleWidth += 0.08;
                  mScaleHeight += 0.05;
                  mRotateAngle += 5;

                  if (mScaleWidth >= 1.0f) mScaleWidth = 0.1f;
                  if (mScaleHeight >= 1.0f) mScaleHeight = 0.1f;
                  if (mRotateAngle >= 90) mRotateAngle = 0;

              mResizedBitmap.recycle();
              TransformThePicture();

              return (true);
              }

           return super.onKeyDown(keyCode, event);
        }

    private Bitmap mBitmapOrg;
    private float mScaleWidth;
    private float mScaleHeight;
    private int mRotateAngle;
    private ImageView mImageView;
    private Bitmap mResizedBitmap;
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to