Tuesday, January 4, 2011

Save the camera image using MediaStore

Further work on the last exercise "Implement takePicture function of Android Camera". Modify onPictureTaken() method to save the captured image in Android standard location for images, using Android's MediaStore.

Save the camera image using MediaStore

 PictureCallback myPictureCallback_JPG = new PictureCallback(){

public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
/*Bitmap bitmapPicture
= BitmapFactory.decodeByteArray(arg0, 0, arg0.length); */

Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());

OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriTarget);

"Image saved: " + uriTarget.toString(),

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block


Download the files.

- Download the project

- Start Camera auto-focusing, autoFocus()


Syed Nizaqat Ali (Software Engineer) said...

I appreciate your nice work.

inotfortoni said...

i test it in the phone with android 2.2 but i don't see the pictures saved
what's wrong?

inotfortoni said...

my phone is a HTC inspire 4g

Android Er said...

hello inotfortoni,

Try the proect: Download the project.

Tested on Nexus One.

arun said...

Thank you very much.... Great tutorial... I searched so many sites for camera application.. am satisfied with your tutorial.. I tried your code and its working..
Thanks again...

Bill Soriano said...

Excellent tutorial, thanks for sharing your knowledge, I have a
query ... I add an opaque background and an image on the view (macro)...but when I
take a photo not recorded...this is the image http://dl.dropbox.com/u/46059390/fotos/imageOverlayAndroid.JPG
the upper image is in main.xml and the other in the same control.xml ...have any idea how to do this

Anonymous said...

App crashes when clicking the button, unless you add

to the manifest.
Great tutorial!

widi said...

how to show the image capture in imageview ?

Andr.oid Eric said...

hello widi,

In the code, u know where the image saved. Such that you can load the image from it. Refer to Scale bitmap Efficiently.

vanVCLenn said...

Very well-explained tutorial...

now I can make a working front camera app by following your step by step explanation

I Like it :D

ugur said...

Best tutorial on camera.Great work.

I got a question. Lets say i took a picture and saved it then i wanted to leave AndroidCamera actiity and start another one.
How can i do that? I've tried many ways but it always crashes. I guess it's because of SurfaceView.

Can you please help me about that?

Quang Anh Đào Ngọc said...

tks for tutorial. But how can I take byte[] from a picture to so something else?

I have public byte[] and do this
Camera.PictureCallback myPictureCallback_JPG = new Camera.PictureCallback(){

public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
pictureBin = arg0;
bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);


But it's alway null when I take picture ??

Unknown said...

Why the pictures I take are black?