0

How can I offer the user the option of choosing an image from the camera or gallery and retrieve the Resource id associated?

Addev
  • 31,819
  • 51
  • 183
  • 302
  • Take a look to this answer with an intent that merges both requests (Camera & Gallery) in a unique Intent: http://stackoverflow.com/a/32475805/2232889 – Mario Velasco Sep 09 '15 at 09:30
  • Does this answer your question? [How to pick an image from gallery (SD Card) for my app?](https://stackoverflow.com/questions/2507898/how-to-pick-an-image-from-gallery-sd-card-for-my-app) – General Grievance Aug 30 '22 at 20:59

1 Answers1

5

Try this as an intent

Intent i = new Intent(Intent.ACTION_PICK,
           android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);

Here is how you retreive the returned image.

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

switch(requestCode) { 
case REQ_CODE_PICK_IMAGE:
    if(resultCode == RESULT_OK){  
        Uri selectedImage = imageReturnedIntent.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();


        Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
    }
}
}

(code from https://stackoverflow.com/a/2508138)

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
coder_For_Life22
  • 26,645
  • 20
  • 86
  • 118
  • Also you can get more info here http://stackoverflow.com/questions/2507898/how-to-pick-a-image-from-gallery-sd-card-for-my-app-in-android – coder_For_Life22 Dec 14 '11 at 00:45
  • Thanks for your answers, but I dont need the Bitmap at this moment, I'm handling an int[] with the app's image resources and I want to add the image selected by the user – Addev Dec 14 '11 at 00:59
  • The code i give you allows you to add the image selected by the user. – coder_For_Life22 Dec 14 '11 at 01:02
  • this code only picks images from gallery, and does not give the user an option to capture an image from the camera like the original question suggested. – Silvia H May 28 '15 at 11:22