Capture Image or Choose from Gallery/Photos Implementation for Android

As for an Android beginner, to test things out, you would need a simple and clear guide.

As you can find, there are many articles, blogs, and guides on this topic. But most of them are not working and or haven’t tested or too complex to understand. Let me take you through a guide on how to implement this very easily.

First, you must have a ImageView in your layout implemented to capture the image you upload either through the camera or Gallery.

Following is my ImageView implementation for the above purpose.

    app:layout_collapseParallaxMultiplier="0.7" />

Now in the activity method, you have to declare the ImageView first.

private ImageView imageView;

In the onCreate override method, you have to initialize the ImageView element by adding the following,

imageView = (ImageView) findViewById(<<id of the ImageView>>);

Now you have to include the following method, which lets the user to choose how he/she want to upload the picture, either through camera or Gallery/Photos.

private void selectImage(Context context) {
    final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Choose your profile picture");

    builder.setItems(options, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int item) {

            if (options[item].equals("Take Photo")) {
                Intent takePicture = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(takePicture, 0);

            } else if (options[item].equals("Choose from Gallery")) {
                Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(pickPhoto , 1);

            } else if (options[item].equals("Cancel")) {

This allows showing the following dialog box,

Now you have to implement the onActivityResult override method as follows,

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode != RESULT_CANCELED) {
        switch (requestCode) {
            case 0:
                if (resultCode == RESULT_OK && data != null) {
                    Bitmap selectedImage = (Bitmap) data.getExtras().get("data");

            case 1:
                if (resultCode == RESULT_OK && data != null) {
                    Uri selectedImage =  data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    if (selectedImage != null) {
                        Cursor cursor = getContentResolver().query(selectedImage,
                                filePathColumn, null, null, null);
                        if (cursor != null) {

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


Now you have to call the selectImage method from the onCreate method.

Tadhaaa!! Yes, that is all you have to do!!

Good Luck with your Android App…

You can find the code from here

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s