Search code examples
androidandroid-imageexifandroid-camera-intentandroid-external-storage

Image file missing exif data


Using intent to take a photo, which is later uploaded to a server. Image is first saved to sdcard like so:

/**
 * Creating file uri to store image/video
 */
public Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

/**
 * returning image / video
 */
private static File getOutputMediaFile(int type) {

    // External sdcard location
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            Config.IMAGE_DIRECTORY_NAME);

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(TAG, "Oops! Failed create "
                    + Config.IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    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;
}

The saved image has no exif data. Is it due to the uri? (photos taken with the default camera app has exif data so it has to be an issue in my app).

Edit: Code used to launch default camera app:

/**
 * Launching camera app to capture image
 */
private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

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

url stored:

/**
 * Here we store the file url as it will be null after returning from camera
 * app
 */
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    // save file url in bundle as it will be null on screen orientation
    // changes
    outState.putParcelable("file_uri", fileUri);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    // get the file url
    fileUri = savedInstanceState.getParcelable("file_uri");
}

Source: http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/


Solution

  • photos taken with the default camera app has exif data so it has to be an issue in my app

    Not necessarily. From the code that you have shown, it would appear to be a bug in that particular camera app. Not all camera app developers test ACTION_IMAGE_CAPTURE very well. Unless you are modifying the image sometime after the picture is taken but before you run your EXIF test, the camera app is not adding EXIF tags to pictures taken via ACTION_IMAGE_CAPTURE. These things happen.

    You might install some camera apps and see how they behave with ACTION_IMAGE_CAPTURE. Open Camera, for example, supports ACTION_IMAGE_CAPTURE, though I have not tested its EXIF behavior when started with ACTION_IMAGE_CAPTURE.