Gallery intent tutorial
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:
- private static Bitmap rotateImage = null;
- private ImageView imageView;
- private static final int GALLERY = 1;
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.


We process the result in the onActivityResult method, which is called automatically when the gallery is finished.
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (requestCode == GALLERY && resultCode != 0) {
- Uri mImageUri = data.getData();
- try {
- if (getOrientation(getApplicationContext(), mImageUri) != 0) {
- Matrix matrix = new Matrix();
- matrix.postRotate(getOrientation(getApplicationContext(), mImageUri));
- if (rotateImage != null)
- rotateImage.recycle();
- imageView.setImageBitmap(rotateImage);
- } else
- e.printStackTrace();
- e.printStackTrace();
- }
- }
- }
The getOrientation method, used above returns the angle the image was taken. So if it was made with a rotated phone we must rotate the image before we can correctly display it in an ImageView. It can me implemented with the help of the Android MediaStore.

Download code: LINK
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@Idevicegazette (iDevice Gazette)
GSM-to-Skype bridge lets you lose those roaming fees http://bit.ly/lbRJeh #android
9 years 36 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
9 years 36 weeks ago -
@dwilliams5 (Dennis Williams)
just completed a runtastic run of 3.02 km in 40 min 11 s with #runtastic #Android App: http://tinyurl.com/5tvrpe3
9 years 36 weeks ago -
@S_Pinz (Spinz!)
RT @Androidheadline: Out of box #LG Optimus 3D got Quadrant 2420 score. Thanks @blink_c #io2011 #android http://twitpic.com/4whkdz
9 years 36 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
9 years 36 weeks ago