Skip to content

Instantly share code, notes, and snippets.

@ccabanero
Last active February 25, 2016 12:13
Show Gist options
  • Save ccabanero/7000022 to your computer and use it in GitHub Desktop.
Save ccabanero/7000022 to your computer and use it in GitHub Desktop.
Android - invoking the camera app with an Intent
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class YourActivity extends Activity implements OnClickListener {
ImageButton imageButtonCamera;
private static final int CAMERA_PIC_REQUEST = 1337;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set the layout
setContentView(R.layout.yourLayout);
//initialize camera image button
this.initializeCameraImageButton();
}
private void initializeCameraImageButton() {
imageButtonCamera = (ImageButton)findViewById(R.id.imageButtonCamera);
imageButtonCamera.setOnClickListener(this);
}
//-----------------------------------
// Summary: OnClickListener methods
//-----------------------------------
@Override
public void onClick(View v) {
//create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//start the image capture Intent
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
//-----------------------------------------------------------------------------------
// Summary: For receiving the result of the intent (after camera app takes picture)
//-----------------------------------------------------------------------------------
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if( requestCode == CAMERA_PIC_REQUEST)
{
//get the picture as a bitmap
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
//update the image of the camera icon to now show the picture that was taken
imageButtonCamera.setImageBitmap(thumbnail);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment