35

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?

ninjasense
  • 13,756
  • 19
  • 75
  • 92
  • Hello all, i want to capture Image by the Native camera of Android and store it ..so that i have made bounty becoz troubling with it – Paresh Mayani Aug 16 '10 at 05:17
  • Paresh what are your problems with capturing the image? Is the approach shown in this question not working for you? – Janusz Aug 16 '10 at 07:36
  • @Janusz I have to put one Textview (Activity name), One Button(at bottom) and on middle of the screen,camera preview is there and on button click event, photo should be clicked and photo should be displyed in imageview of another activity – Paresh Mayani Aug 16 '10 at 09:00
  • Then you don't want to use the native android camera application. This question provides an example on how to get an image without displaying the camera preview etc in your own application. – Janusz Aug 16 '10 at 09:15
  • @Janusz then what i do ? if i make one question for it..then public will throwing questions as "duplicate"...and in my application, its needed that "Camera preview" should be displayed to take photo...thanx for the support – Paresh Mayani Aug 16 '10 at 09:24
  • @Paresh - This was a pretty good example I used before I decided on the native camera: http://www.brighthub.com/mobile/google-android/articles/43414.aspx – ninjasense Aug 16 '10 at 15:59
  • @ninjasense does the answer I posted below fix your problem? – stealthcopter Aug 16 '10 at 16:45
  • @stealthcopter I have not yet had a chance to test but I will be sure to let you know the results. – ninjasense Aug 16 '10 at 17:15

5 Answers5

29

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();
        }

    }

}
ninjasense
  • 13,756
  • 19
  • 75
  • 92
21

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";
stealthcopter
  • 13,964
  • 13
  • 65
  • 83
14

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.

Community
  • 1
  • 1
Fedor
  • 43,261
  • 10
  • 79
  • 89
1

You should perform a media scanning after saving the image

 sendBroadcast(new Intent(
            Intent.ACTION_MEDIA_MOUNTED,
            Uri.parse("file://" + Environment.getExternalStorageDirectory())));
KP_
  • 1,854
  • 25
  • 43
-7

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.

mbinette
  • 5,094
  • 3
  • 24
  • 32
Gurdev
  • 5