views:

46

answers:

1

Hello,

As of right now, in my app I have created a rudimentary gallery app using the provided widget, I need this to select a picture from the phone. This is working fine and everything, but lacking very much in presentation.

I've got a couple apps on my phone that do the same thing, but they somehow use the gallery that's already in the phone to let the user select an image. FourSquare, for example, when you select an image to use as your picture, it loads the gallery and asks you to select an image.

How is this possible? I've scoured the internet for the last couple and have come up empty handed.

A: 

To get an image from the standard gallery you can do:

private static final int MEDIA_IMAGE_REQUEST_CODE = 203948; // This can be any unique number you like

Intent getImageFromGalleryIntent = 
  new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(getImageFromGalleryIntent, MEDIA_IMAGE_REQUEST_CODE);

Then to receive the image once the user has chosen one:

protected final void onActivityResult(final int requestCode, final int resultCode, final Intent i) {
  super.onActivityResult(requestCode, resultCode, i);
  if(resultCode == RESULT_OK) {
    switch(requestCode) {
      case MEDIA_IMAGE_REQUEST_CODE:

        // Get the chosen images Uri
        Uri imageUri = i.getData();

        // Load the bitmap data from the Uri 
        // It is probably best to do this in an AsyncTask to avoid clogging up the Main Thread
      break;
    }
  }
disretrospect