Android Camera
Android Camera
• If you want to add to our apps the capability to take photos using
the integrated smart phone camera, then the best way is to use an
Intent.
• For example, let us suppose you want to start the camera as soon
as we press a button and show the result in our app.
• In the onCreate method of our Activity, setup a listener of the
Button and when clicked to fire the intent:
1
Button b = (Button) findViewById(R.id.btn1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Here we fire the intent to start the camera
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 100);
}
});
2
In the onActivityResult method, we retrieve the picture taken and
show the result:
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data)
{
// This is called when you finish taking the photo
Bitmap bmp = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(bmp);
}
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35