PocketMagic

PocketMagic

Where Technology meets magic


Android
45 Posts
BlackBerry
4 Posts
Electronics
68 Posts
Hardware
120 Posts
High Voltage
49 Posts
iPhone
4 Posts
Linux
2 Posts
Nuclear
20 Posts
Optics
11 Posts
Photography
7 Posts
Photoshop
3 Posts
Research
19 Posts
Reviews
18 Posts
Robotics
8 Posts
Security
7 Posts
Software
73 Posts
Symbian
2 Posts
Tubes
18 Posts
Windows
3 Posts
Windows Mobile
11 Posts

Top Articles!       See PocketMagic on Facebook


uRADMonitor - Online Radiation monitoring station | 15006 Views | Rate 69.8
uRADMonitor - Online Radiation monitoring station
Bluetooth and iOS - Use Bluetooth in your iPhone apps | 18515 Views | Rate 59.34
Bluetooth and iOS - Use Bluetooth in your iPhone apps
NMEA GPS Library for AVR Microcontrollers | 4890 Views | Rate 55.57
NMEA GPS Library for AVR Microcontrollers
Programmatically Injecting Events on Android - Part 2 | 5144 Views | Rate 45.12
Programmatically Injecting Events on Android - Part 2
Building a robot – Part 2 | 4698 Views | Rate 44.32
Building a robot – Part 2
Simple Switched power Supplies | 16328 Views | Rate 42.08
Simple Switched power Supplies
Capacitor Discharge Microspot Welder / Cutter | 11460 Views | Rate 36.5
Capacitor Discharge Microspot Welder / Cutter
Atmega8 and Nokia 5110 LCD  | 1587 Views | Rate 34.5
Atmega8 and Nokia 5110 LCD

 
  

Android PhotoPicker using Intents and Gallery


By Radu Motisan Posted on February 2nd, 2011 , 1896 Views (Rate 2.25)



  




Sometimes you need to select a photo. To make it convenient for the user, you should search all the images on the device, generate thumbnails, create a touch&go interface, and a lot more details, that take some considerable time to implement. Luckily we can use the default Android Gallery instead, and invoke it from withing our application:

The code to achieve this is very easy and straightforward. For the demo app, I've created a LinearLayout that holds a TextView (for displaying messages) a Button (to select the image) and an ImageView (to display the image selected). Pressing the button, invokes the Gallery intent, for ACTION_PICK / image picking.

  1.  
  2. Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
  3. photoPickerIntent.setType("image/*");
  4. startActivityForResult(photoPickerIntent, idIntentID);
  5.  

We start this intent using startActivityForResult, passing the idIntentID variable. We also need to make sure the main class overrides onActivityResult. By doing so, we will receive a notification when the user has chosen a picture or has canceled the Photo picker:

  1.  
  2. @Override
  3. protected void onActivityResult(int requestCode, int resultcode, Intent intent)
  4. {
  5. super.onActivityResult(requestCode, resultcode, intent);
  6. if (requestCode == idIntentID) {
  7. if (intent != null) {
  8. Log.d(LOG_TAG, "idButSelPic Photopicker: " + intent.getDataString());
  9. Cursor cursor = getContentResolver().query(intent.getData(), null, null, null, null);
  10. cursor.moveToFirst(); //if not doing this, 01-22 19:17:04.564: ERROR/AndroidRuntime(26264): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
  11. int idx = cursor.getColumnIndex(ImageColumns.DATA);
  12. String fileSrc = cursor.getString(idx);
  13. Log.d(LOG_TAG, "Picture:" + fileSrc);
  14. m_Tv.setText("Image selected:"+fileSrc);
  15.  
  16. Bitmap bitmapPreview = BitmapFactory.decodeFile(fileSrc); //load preview image
  17. BitmapDrawable bmpDrawable = new BitmapDrawable(bitmapPreview);
  18. m_Image.setBackgroundDrawable(bmpDrawable);
  19. }
  20. else {
  21. Log.d(LOG_TAG, "idButSelPic Photopicker canceled");
  22. m_Tv.setText("Image selection canceled!");
  23. }
  24. }
  25. }
  26.  

It's easy, but very useful for many applications. Here is the sample project code:
PhotoPicker






  

More on PocketMagic:

How to set the AVR Fusebits | 1766 Views | Rate 23.86
How to set the AVR Fusebits
ATMega128 and HD44780 LCD using 3 Wires with the 74HC164 | 2100 Views | Rate 22.83
ATMega128 and HD44780 LCD using 3 Wires with the 74HC164
Repairing a Victoreen CDV-700 6B Dosimeter  | 179 Views | Rate 22.38
Repairing a Victoreen CDV-700 6B Dosimeter
Dual H-Bridge for controlling two motors | 1258 Views | Rate 21.69
Dual H-Bridge for controlling two motors
USBAsp -  AVR USB Programmer  | 8204 Views | Rate 21.2
USBAsp - AVR USB Programmer
Developing for Blackberry 10 | 99 Views | Rate 19.8
Developing for Blackberry 10

2 Responses to “Android PhotoPicker using Intents and Gallery”

  1. 1
    Rasa Rahul:

    in the above example it displays all the images present in the phone instead is tehre any way to display images from particular directory????

  2. 2
    Divya:

    Thanks a lot.It really helped me,but I m getting a problem with this.
    When i add new image in sdcard,it doesnt show that in the photopicker.

Thank you for commenting. Your comment won't show until approved, which can take some time.

Please copy the string eGtgLZ to the field below: