Posts tagged ACTION_IMAGE_CAPTURE
Take Photo Using ACTION_IMAGE_CAPTURE Intent
0In this post we will build a working sample app that allows the user to press a button to take a photo using the phone’s camera app (or any other available) and then loads this photo into an ImageView. You could of course do anything you like with the photo such as upload it to a web service or save it to permanent storage.
Begin by creating a new Android project, then open up your main.xml layout file and edit it. The below xml creates an ImageButton and an ImageView. Everything we need for the sample. The ImageView references a placeholder image.. just a little snap of someone I spotted the last time I was at the beach.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:background="#FFFFFF">
<ImageButton
android:id="@+id/btn_take_photo"
android:src="@android:drawable/ic_menu_camera"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp" />
<ImageView
android:id="@+id/img_main"
android:src="@drawable/kiss_from_a_rose"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:padding="5dp" />
</LinearLayout>
In your Activity’s onCreate() method, attach a listener to the button. When the button is clicked we want to start an ACTION_IMAGE_CAPTURE Intent using startActivityForResult().
findViewById(R.id.btn_take_photo).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, REQ_CODE_PHOTO_TAKE);
}
});
You’ll also need to create the REQ_CODE_PHOTO_TAKE constant. This value will be returned in the resultCode.
private static final int REQ_CODE_PHOTO_TAKE = 1;
Next up push Alt+Shift+S and click “Override/Implement Methods” from the menu. Choose to Override onActivityResult() and click ok.
You’ll notice this method has three parameters. ‘resultCode’ is self explanatory – if this is anything other than RESULT_OK we’ll want to return immediately. An example where this might happen is if the user were to push the button to take a photo but then push their phone’s back button immediately rather than take one. requestCode contains the value that we passed in during startActivityForResult(), in our case this is REQ_CODE_PHOTO_TAKE. Lastly is the returned Intent. We can use this to get a Uri to the data by calling getData().
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode != RESULT_OK) return;
switch (requestCode) {
case REQ_CODE_PHOTO_TAKE:
Uri u = data.getData();
onPhotoReturned(u);
break;
default:
break;
}
}
Lastly we’ll want to display the photo in our ImageView. Create the new method, onPhotoReturn(Uri u). We use the Uri to open an InputStream and from this we can create a BitmapDrawable which can be used to set the Drawable for the ImageView.
private void onPhotoReturned(Uri u) {
InputStream is = null;
try {
ContentResolver cResolver = getContentResolver();
is = cResolver.openInputStream(u);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BitmapDrawable bmp = new BitmapDrawable(is);
ImageView mainImage = (ImageView)findViewById(R.id.img_main);
mainImage.setImageDrawable(bmp);
}
Working code for this app can be found on my github and a screenshot of it in action is below:
