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

48,927

Solution 1

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:

 /mnt/sdcardmake_machine_example.jpg

When what you really want is:

 /mnt/sdcard/make_machine_example.jpg

Try this code instead:

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

Solution 2

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";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        try {

            super.onCreate(savedInstanceState);         
                    File root = new File(Environment
                            .getExternalStorageDirectory()
                            + File.separator + "myDir" + File.separator);
                    root.mkdirs();
                    sdImageMainDirectory = new File(root, "myPicName");


                startCameraActivity();
            }
        } catch (Exception e) {
            finish();
            Toast.makeText(this, "Error occured. Please try again later.",
                    Toast.LENGTH_SHORT).show();
        }

    }

    protected void startCameraActivity() {

        Uri outputFileUri = Uri.fromFile(sdImageMainDirectory);

        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
        case 0:
            finish();
            break;

        case -1:

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

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

        }

    }

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

    @Override
    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);
            bm.recycle();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

Solution 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.

Solution 4

You should perform a media scanning after saving the image

 sendBroadcast(new Intent(
            Intent.ACTION_MEDIA_MOUNTED,
            Uri.parse("file://" + Environment.getExternalStorageDirectory())));
Share:
48,927
ninjasense
Author by

ninjasense

Updated on January 03, 2020

Comments

  • ninjasense
    ninjasense over 4 years

    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?

  • Mathias Conradt
    Mathias Conradt over 13 years
    yes, I can confirm that you will need to add the file separator
  • Vincent
    Vincent about 13 years
    Do you need to set extra permissions in the manifest? I can't give my own name to the image file saved to sd
  • ninjasense
    ninjasense about 13 years
    yes...<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  • iankits
    iankits over 12 years
    Hi Ninja!! May I know, what is "Home.class"???? its saying to create a class/enum etc. i am new to android programming.
  • ocodo
    ocodo about 11 years
    A bit past sell-by but there's an additional closing brace on the first try / catch block. I'm going to extract this out to a gist, unless you have any objections. @ninjasense
  • Android is everything for me
    Android is everything for me almost 10 years
    am using same code but i want to show preview to user is anyone know how to show preview to user before proceeding on that image?
  • W I Z A R D
    W I Z A R D about 9 years
    it will not store captured image.
  • rupesh
    rupesh almost 9 years
    onActivityResult is not getting called in my case. it is saving the image in directory but not going to next Activity which is written in onActivityResult method
  • rupesh
    rupesh almost 9 years
    @ninjasense please help me on this