How to capture an image and store it with the native Android Camera

The question:

I am having a problem capturing an image and storing it from the native camera app. Here is a sample of some of my code.

_path = Environment.getExternalStorageDirectory() + "make_machine_example.jpg";
File file = new File( _path );
Uri outputFileUri = Uri.fromFile( file );

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );

startActivityForResult( intent, 0 );

After the picture has been taken and I’m returned back to my original Activity, When I navigate to my sd card via Android DDMS File Explorer the picture is not there. Anyone know why this is not being saved?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Here was the final product in case anyone is still visiting this thread:

public class CameraCapture extends Activity {

    protected boolean _taken = true;
    File sdImageMainDirectory;

    protected static final String PHOTO_TAKEN = "photo_taken";

    public void onCreate(Bundle savedInstanceState) {

        try {

                    File root = new File(Environment
                            + File.separator + "myDir" + File.separator);
                    sdImageMainDirectory = new File(root, "myPicName");

        } catch (Exception e) {
            Toast.makeText(this, "Error occured. Please try again later.",


    protected void startCameraActivity() {

        Uri outputFileUri = Uri.fromFile(sdImageMainDirectory);

        Intent intent = new Intent("");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

        startActivityForResult(intent, 0);

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
        case 0:

        case -1:

            try {
                StoreImage(this, Uri.parse(data.toURI()),
            } catch (Exception e) {

            startActivity(new Intent(CameraCapture.this, Home.class));



    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        if (savedInstanceState.getBoolean(CameraCapture.PHOTO_TAKEN)) {
            _taken = true;

    protected void onSaveInstanceState(Bundle outState) {
        outState.putBoolean(CameraCapture.PHOTO_TAKEN, _taken);

        public static void StoreImage(Context mContext, Uri imageLoc, File imageDir) {
        Bitmap bm = null;
        try {
            bm = Media.getBitmap(mContext.getContentResolver(), imageLoc);
            FileOutputStream out = new FileOutputStream(imageDir);
            bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } catch (Exception e) {



Method 2

Have you checked what the output of Environment.getExternalStorageDirectory() is, because if it does not contain a trailing file seperator (/) then your image will end up in a directory that does not reside on the SDcard such as:


When what you really want is:


Try this code instead:

 _path = Environment.getExternalStorageDirectory() + File.separator +  "make_machine_example.jpg";

Method 3

1 . Just use

new File(Environment.getExternalStorageDirectory(),  "make_machine_example.jpg");

and don’t bother about separators.

2 . Faced the same problem before. SenseUI phones have a custom camera application that doesn’t create file. What device are you using? It may already be fixed in latest devices but it may also still be an issue. So here’s a complete sample how to overcome it Problems saving a photo to a file.

Method 4

You should perform a media scanning after saving the image

 sendBroadcast(new Intent(
            Uri.parse("file://" + Environment.getExternalStorageDirectory())));

Method 5

Add this line into AndroidManifest.xml file and remove extension make_machine_example:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

It will start to capture the Photo and store into the SDcard.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment