How to Create Android Image Picker

In my previous tutorial on how to select  and crop image on Android, i’ve explained how to create image picker/chooser to select an image from sdcard or camera then crop the selected image. The tutorial is quite advanced and on many cases in our application, we only need to select the image without cropping it. So i made a simplified example to show how to create an image chooser.

public class MainActivity extends Activity {
	private Uri mImageCaptureUri;
	private ImageView mImageView;

	private static final int PICK_FROM_CAMERA = 1;
	private static final int PICK_FROM_FILE = 2;

    public void onCreate(Bundle savedInstanceState) {


        final String [] items			= new String [] {"From Camera", "From SD Card"};
		ArrayAdapter<String> adapter	= new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items);
		AlertDialog.Builder builder		= new AlertDialog.Builder(this);

		builder.setTitle("Select Image");
		builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
			public void onClick( DialogInterface dialog, int item ) {
				if (item == 0) {
					Intent intent 	 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
					File file		 = new File(Environment.getExternalStorageDirectory(),
							   			"tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
					mImageCaptureUri = Uri.fromFile(file);

					try {
						intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
						intent.putExtra("return-data", true);

						startActivityForResult(intent, PICK_FROM_CAMERA);
					} catch (Exception e) {

				} else {
					Intent intent = new Intent();


	                startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
		} );

		final AlertDialog dialog = builder.create();

		mImageView = (ImageView) findViewById(;

		((Button) findViewById( View.OnClickListener() {
			public void onClick(View v) {;

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    if (resultCode != RESULT_OK) return;

		Bitmap bitmap 	= null;
		String path		= "";

		if (requestCode == PICK_FROM_FILE) {
			mImageCaptureUri = data.getData();
			path = getRealPathFromURI(mImageCaptureUri); //from Gallery

			if (path == null)
				path = mImageCaptureUri.getPath(); //from File Manager

			if (path != null)
				bitmap 	= BitmapFactory.decodeFile(path);
		} else {
			path	= mImageCaptureUri.getPath();
			bitmap  = BitmapFactory.decodeFile(path);


	public String getRealPathFromURI(Uri contentUri) {
        String [] proj 		= {MediaStore.Images.Media.DATA};
        Cursor cursor 		= managedQuery( contentUri, proj, null, null,null);

        if (cursor == null) return null;

        int column_index 	= cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);


        return cursor.getString(column_index);

The code from my sample project above shows how to create a dialog that shows two options to select the image source. The first is from Camera, where the picture is taken directly from camera, and the second is from saved images on sdcard.

line 14: Define two image source options, From Camera and From SD Card.

line 16-48: Setup the image picker dialog.

line 22: If user choose to take picture from Camera, create an intent to open Camera app with MediaStore.ACTION_IMAGE_CAPTURE action.

line 23: Create temporary file to hold the image from Camera.

line 24: Get the uri of temporary file

line 39: If user choose to select image from sdcard, start the intent to open image chooser dialog. The image chooser dialog will display list File Manager (if exist) apps and default Gallery app.

line 55: show the image picker dialog.

android image picker android image picker

line 61: Override the onActivityResult method to handle the selected image.

line 67: If user selects image from sdcard, get the uri of selected image (line 68)

line 69: Assume user selects the image from sdcard using Gallery app. The uri from Gallery app does not give the real path to selected image, so it has to be resolved on content provider. Method getRealPathFromURI used to resolve the real path from the uri.

line 71: If the path is null, assume user selects the image using File Manager app. File Manager app returns different information than Gallery app. To get the real path to selected image, use getImagePath method from the uri (line 72).

line 75: Get the bitmap.

line 77: If user choose to take picture from camera, get the real path of temporary file that previously defined on line 24-25.

line 78: Get the bitmap.

line 81: Display the bitmap on image view.

android image picker

Download source code of this tutorial on my github page

facebooktwittergoogle_plusredditpinterestlinkedinmailby feather


  1. says

    thanks this is a wonderful coding you did but i am facing 1 problem that-

    on clicking of camera it is coming for few second after that it is being close with force close,please tell me the solution??

  2. krishna kanth says

    hank u so much,so your coding is better and more dynamic. But i have one issue,while using devices with api level above 4, always it is the returning the pic in LANDSCAPE mode. How to solve this problem ??

  3. Yugie says

    Hallo mas londa, mas saya mau tanya kalau selanjutnya file itu mau dikirim ke server gmna ya? kirim via HTTP Post nya seperti apa ya? Request tutorialnya mas, Thanks Mas.. :)

  4. says

    Thanks for your example. It works almost flawlessly. I have one problem though; When taking a picture via camera intent, it works perfectly in landscape mode, but in portrait mode it somehow loses its data and crashes for null data. Im using galaxy tab 10.1

  5. says

    great tutorial…I have just one problem please help me…How can I store the image path chosen from SD card to sqlite database….plz help

  6. devang says

    thank you very very much, but can u help me in storing this image in sqlite3? please upload a tutorial for creating the database for storing the captured image.thankz in advance

  7. says

    Thank you so much! Worked for me (I only use the part for choosing from file manager though)
    btw, in line 86, there’s:
    Cursor cursor = managedQuery( contentUri, proj, null, null,null);
    but since it’s deprecated, I use:
    Cursor cursor = getContentResolver().query( contentUri, proj, null, null,null);
    it worked, though I don’t know if there’s any side effect…

  8. Rahman says

    Hi. Thanks for your tutorial. However I got an error on onActivityResult method. It says syntax errors on token, or misplaced contract in the method parameter resultCode. Do you have any idea about this?

    Thank you

      • Rahman says


        Thanks for reply. I am using version 4.4. But now, the problem is solved as I reposition the method in class. No errors at all.

        Hoewever, when I take picture from camera or sd card, my imageview object does not display it. Can you give me your advice? Thanks a lot.

  9. ajit says

    what is this “”, it cannot be resolved in eclipse. Whats the use of it ?
    Same for “” ….

    please do reply

  10. Tanka says

    For picking image below does not work in mine.

    Intent intent = new Intent();


    startActivityForResult(Intent.createChooser(intent, “Complete action using”), PICK_FROM_FILE);

    Instead I replace with :
    Intent intent = new Intent(

    “Complete action using”), SELECT_FILE);

    This worked for me

Leave a Reply

Your email address will not be published. Required fields are marked *