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
-
@StephanieNich10 (Stephanie Nichols)#android I laughed so hard at ochocinco 's avi. Hahahaha http://t.co/MPuhhi3m
25 weeks 2 days ago -
@CarlaAtkins8 (Carla Atkins)#android Omg! This is actually f'n interesting http://t.co/JodMOehr
25 weeks 2 days ago -
@MarianMcleod12 (Marian Mcleod)#android Precisely what song is? http://t.co/YmJXU0rB
25 weeks 2 days ago -
@JoBeach15 (Jo Beach)#android haha this made me laugh, i love ted:-) http://t.co/gtcWQ79C
25 weeks 2 days ago -
@aochart3 (青ちゃ)Start playing Paradise Island on Android http://t.co/DEID0Ao5 #Android #Androidgames #Gameinsight http://t.co/e1bifSeL
25 weeks 2 days ago
Poll
Useful resources
Android Development Projects
- Brandable Voip Mobile dialers - repost by muphys
- Make iOS Application from Existing Android Application, GPS, Music, TTS Experience Required by oliveseraph
- mobile affiliate program by monofeo
- iphone and android application by asifyouseef
- Iphone and Android APP Driving Service Locator. by ryanbeatty
- Android Soundboard App by M24Designs
- iOS & Android APPs Developer needed by trapmasked
- REMOTE INSTALL GPS TRACKING OF LOST/STOLEN MOBILE PHONE - repost by webologic
- amendment to Iphone face morphing application and migration to android application by prashantshukla83
- web and android application using catchoom by krball



