0

Hi i started to use Camera in Android, as simple as it can get from here.

The problem is after i shoot the photo the file's uri is just null for some reason.

Steps:

  1. I ran the takePhoto() func, my phone's standard camera app starts, i take a photo, i save it.

  2. My app ran the onActivityResult(...) func as it gets back the control from camera, and the file's uri is just null.

Code:

In my Activity:

/**
 * Intent code for capturing a photo
 */
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

/**
 * 
 * Takes a photo
 */
public void takePhoto() {
    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    fileUri = CameraHelper.getOutputMediaFileUri(CameraHelper.MEDIA_TYPE_IMAGE); // create a file to save the image
    Log.i("PHOTO", "file uri is: " + fileUri.toString());
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
/**
 * 
 * After taking the photo...
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
            if (data == null) {
                Toast.makeText(this, "Camera error: data is null!", Toast.LENGTH_LONG).show();
                Log.i("PHOTO", "Camera error: data is null!");
                // Error is here, data does not contains the uri.
            }
            else {
                Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
                Log.i("PHOTO", "Image saved to:\n" + data.getData());
            }
        }
        else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        }
        else {
            // Image capture failed, advise user
        }
    }
}

CameraHelper class:

public class CameraHelper {

public static final int MEDIA_TYPE_IMAGE = 1;



/** Create a file Uri for saving an image or video */
public 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()) {

        Log.i("PHOTO", "directory is not exists yet!");
        if (!mediaStorageDir.mkdirs()) {
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
        else {
            Log.i("PHOTO", "just made directory: " + mediaStorageDir.getAbsolutePath());
        }
    }
    else {
        Log.i("PHOTO", "directory is already exist: " + mediaStorageDir.getAbsolutePath());
    }
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    }
    else {
        return null;
    }
    return mediaFile;
}

}

Log output:

01-13 16:15:37.485: I/PHOTO(6818): directory is already exist: /storage/sdcard0/Pictures/MyCameraApp
01-13 16:15:37.495: I/PHOTO(6818): file uri is: file:///storage/sdcard0/Pictures/MyCameraApp/IMG_20150113_161537.jpg


01-13 16:16:13.482: I/PHOTO(6818): Camera error: data is null!

All the pictures are in the folder: Pictures/MyCameraApp but onActivityResult(...) does not contains the uri in the data field.

What m i doing wrong?

Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222
  • Here's an excellent description: http://stackoverflow.com/questions/10042695/how-to-get-camera-result-as-a-uri-in-data-folder – kelvincer Jan 13 '15 at 15:43
  • Without using EXTRA_OUTPUT camera application returns a non-null intent returning back a thumbnail in the returned intent. If you pass EXTRA_OUTPUT with a URI, it will return a null intent and the picture is in the URI that you passed in. – vinv Jan 13 '15 at 17:11

0 Answers0