Java – Motorola Android 2.2 cameras ignore EXTRA_OUTPUT parameters

Motorola Android 2.2 cameras ignore EXTRA_OUTPUT parameters… here is a solution to the problem.

Motorola Android 2.2 cameras ignore EXTRA_OUTPUT parameters

I programmatically turn on the camera to shoot video. I told the camera to put the video file in the specified location using code like this:

Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
File out = new File("/sdcard/camera.mp4");
Uri uri = Uri.fromFile(out);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, GlobalUtility.CAMERA_VIDEO);

It works well on HTC phones. But on my moto defy it just ignores the MediaStore.EXTRA_OUTPUT parameter and puts the video in the default position.
Then I used this code in the onActivityResult() function to solve the problem:

private String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

String realPath;
try {
    File file = new File("/sdcard/camera.mp4");
    if (!file.exists()) {
        Uri videoUri = data.getData();
        realPath = getRealPathFromURI(videoUri);
    }
} catch (Exception ex) {
    Uri videoUri = data.getData();
    realPath = getRealPathFromURI(videoUri);
}

Hope this helps others.

Solution

Just because /sdcard/ is a phone and a sdcard directory on an Android version doesn’t mean it will be consistent.

You will want to use > Environment.getExternalStorageDirectory() as Frankenstein’s comment suggests. This will always be used to get the directory of the SD card.

You also need to check if the SD card is currently installable because the phone may be in USB storage mode.

Try something like….

if (! Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
    Log.d(TAG, "No SDCARD");
} else {
    File out = new File(Environment.getExternalStorageDirectory()+File.separator+"camera.mp4");     
}

Related Problems and Solutions