Jackson Flint-Gonzales
Jackson Flint-Gonzales

Reputation: 482

Camera Intent, can't save photo to external storage

I'm trying to call the camera via an Intent and save the photo the user takes to the sd card. I'm stealing much of Google's code, but their example for saving a photo is not working for me.

Here's the code I have so far:

public void takePhoto (View view) {
    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            if (data!=null) {
            // Image captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Image saved to:\n" +
                     data.getData(), Toast.LENGTH_LONG).show();
            }
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }
}

From there, a button is pressed, which takes user to the camera, where they then take a picture and confirm it. After confirmation the app returns to the previous activity but there's no sign of the photo being saved anywhere on the device when using this code:

public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;

/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
      return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "MyCameraApp");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date(type));
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    } else if(type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "VID_"+ timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

I have permissions to write external storage, not sure what I'm missing here. Is it where I place the code?

Thanks.

Upvotes: 2

Views: 2411

Answers (2)

DigCamara
DigCamara

Reputation: 5578

A couple of points thus far:

  1. You should remove the argument from your new Date() sentence:

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());or you'll end up with a date in the 1970's

  2. When you call the Intent like intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); you're telling the Intent you're expecting the file to appear at a certain path. That code makes

    if (data!=null) { // Image captured and saved to fileUri specified in the Intent Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show(); } unnecessary (data will always be null).

It remains to be seen whether your file is appearing in the path we've already seen in your log, but it should.

Upvotes: 2

Benoit
Benoit

Reputation: 4599

Did you put the following AndroidManifest permission ?

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

Upvotes: 0

Related Questions