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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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) {
e.printStackTrace();
}
dialog.cancel();
} else {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
}
}
} );
final AlertDialog dialog = builder.create();
mImageView = (ImageView) findViewById(R.id.iv_pic);
((Button) findViewById(R.id.btn_choose)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.show();
}
});
}
@Override
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);
}
mImageView.setImageBitmap(bitmap);
}
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);
cursor.moveToFirst();
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.

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.

Download source code of this tutorial on my github page
Related post:
sir
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??
Thank you very much , seriously thank you very much
thank u so much,so your coding is better to learn pick image for gallery
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 ??
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..
Hi, how if I want to get the image file path?
Thanks!
Great code. Great help ! Cheers. Keep publishing & helping !
Hi!
Just use below code with your path-
File f = new File(mImageCaptureUri.getPath());
Thank’s, Dude..,
It’s realy help me..,
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
This was awesome. I had a much more convoluted piece of code that did something similar
thanks!
Hi!
Great job, thanks very much!
How can I choose images on eclipse to simulate “Select Image From SD Card”?
Good stuff!
Can’t get rid of the distance between the image and the button, any suggestions?
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
this can filtering by name file or n view name file on picker…on show (Intent.ACTION_GET_CONTENT)
Good work, thanks for sharing!
am new at this, can you share the layout xml please ?
Its possible? I want get all content provider and “Get picture from camera” from one menu?
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