0

I had created a small app such that the image which is displaying should rotate when I click the button. I have written the following code:

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;


public class ImageActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }  
    public void Rotate()
    {

        ImageView img = (ImageView)findViewById(R.id.imageView01);
        Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.bharath);
// Getting width & height of the given image.
        int w = bmp.getWidth();
        int h = bmp.getHeight();
// Setting pre rotate to 90
        Matrix mtx = new Matrix();
        mtx.preRotate(90);
// Rotating Bitmap
        Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
        BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);
        img.setImageDrawable(bmd);
    }

Now when I execute the code and click the button to rotate the image, the application is forcing me to close it, mentioning there is an unexpected error. I could not trace and correct. Can any one please help me in this regard?

Tobbe
  • 1,825
  • 3
  • 21
  • 37
Bharath Gupta
  • 344
  • 3
  • 8
  • 20
  • 2
    Please give people credit for their effort to help you and [accept answers](http://meta.stackexchange.com/a/5235/164138) on your previous questions. You haven't accepted a single answer! – THelper Dec 15 '11 at 10:04
  • Can you post some of the exception code from a logger such as logcat? – Davos555 Dec 15 '11 at 10:04
  • where is your button. From which activity you are calling the Rotate() method. – Padma Kumar Dec 15 '11 at 10:22
  • I have called my Rotate () function from main.xml file – Bharath Gupta Dec 15 '11 at 11:05
  • 12-15 16:53:06.860: I/Process(538): Sending signal. PID: 538 SIG: 9 12-15 17:03:32.318: E/AndroidRuntime(411): at android.view.View.performClick(View.java:2485) 12-15 17:03:32.318: E/AndroidRuntime(411): at android.os.Handler.dispatchMessage(Handler.java:92) 12-15 17:03:32.318: E/AndroidRuntime(411): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) is the Exception that I had got in the log file. – Bharath Gupta Dec 15 '11 at 11:36

1 Answers1

0

Lemme guess, you have Button in the layout main.xml, having this attribute android:onClick="Rotate", in which the method defined in the ImageActivity, don't you?

If it is true, rewrite the definition of Rotate() like this: public void Rotate(View v). It should be working fine.

Additionally, you can just use:

img.setImageBitmap(rotatedBMP);

instead of wasting more memory on that BitmapDrawable

Pete Houston
  • 14,931
  • 6
  • 47
  • 60