image rotate

Gallery intent tutorial

SDK Version: 

Our goal is to start the users gallery application, allow him to select an image, and use the chosen image in our application.

API Level 3 required!

We will start the operation on a buttons onclick event, implemented as follows:

  1. private static Bitmap Image = null;
  2. private static Bitmap rotateImage = null;
  3. private ImageView imageView;
  4. private static final int GALLERY = 1;

  1. public void onClick(View v) {
  2.   imageView.setImageBitmap(null);
  3.   if (Image != null)
  4.     Image.recycle();
  5.   Intent intent = new Intent();
  6.   intent.setType("image/*");
  7.   intent.setAction(Intent.ACTION_GET_CONTENT);
  8.   startActivityForResult(Intent.createChooser(intent, "Select Picture"), GALLERY);
  9. }

In order to avoid out of memory errors, we must recycle the previous image when the user presses the button second times or after that, so the returned bitmap is stored as a member variable, so we still have a reference for it when it is needs to be recycled.

Syndicate content