Camcorder activity not working with example code-Collection of common programming errors

I’m trying to use the default camera app to handle recording/taking pictures using intents but can’t seem to get the default code to work properly. http://developer.android.com/guide/topics/media/camera.html

This code works on Ice Cream sandwich but on Gingerbread (2.3) When trying to record video the image/preview is frozen and when I attempt to retake video it crashes.

From the stacktrace it seems like it does support the video format but it explicitly supports it in the documentation…

Any help or thoughts would be greatly appreciated. 🙂

My code:

public class CameraInterface {
    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;

    public static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
    public static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;

    private static Uri fileUri;

    public static void takePicture(Activity activity){
        // 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
        activity.startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    }


    public static void takeVideo(Activity activity){
        //create new Intent
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

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

        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high

        // start the Video Capture Intent
        activity.startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
    }


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

Stack trace:

 E/videocamera(17373)    Couldn't view video file   ///mnt/sdcard/Pictures/MyCameraApp/VID_20120410_151830.mp4              
 E/videocamera(17373)    android.content.ActivityNotFoundException   No Activity found to handle Intent { act=android.intent.action.VIEW dat=file   ///mnt/sdcard/Pictures/MyCameraApp/VID_20120410_151830.mp4 }            
 E/videocamera(17373)       at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java    1409)           
 E/videocamera(17373)       at android.app.Instrumentation.execStartActivity(Instrumentation.java   1379)           
 E/videocamera(17373)       at android.app.Activity.startActivityForResult(Activity.java    2827)           
 E/videocamera(17373)       at android.app.Activity.startActivity(Activity.java 2933)           
 E/videocamera(17373)       at com.android.camera.VideoCamera.startPlayVideoActivity(VideoCamera.java   488)            
 E/videocamera(17373)       at com.android.camera.VideoCamera.onClick(VideoCamera.java  501)            
 E/videocamera(17373)       at android.view.View.performClick(View.java 2485)           
 E/videocamera(17373)       at android.view.View$PerformClick.run(View.java 9080)           
 E/videocamera(17373)       at android.os.Handler.handleCallback(Handler.java   587)            
 E/videocamera(17373)       at android.os.Handler.dispatchMessage(Handler.java  92)         
 E/videocamera(17373)       at android.os.Looper.loop(Looper.java   130)            
 E/videocamera(17373)       at android.app.ActivityThread.main(ActivityThread.java  3683)           
 E/videocamera(17373)       at java.lang.reflect.Method.invokeNative(Native Method)             
 E/videocamera(17373)       at java.lang.reflect.Method.invoke(Method.java  507)            
 E/videocamera(17373)       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java   839)            
 E/videocamera(17373)       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java  597)            
 E/videocamera(17373)       at dalvik.system.NativeStart.main(Native Method)                
 W/dalvikvm(17373)   threadid=1  thread exiting with uncaught exception (group=0x40015560)              
 E/AndroidRuntime(17373)     FATAL EXCEPTION     main               
 E/AndroidRuntime(17373)     java.lang.IllegalArgumentException  Unknown URL file   ///mnt/sdcard/Pictures/MyCameraApp/VID_20120410_151830.mp4          
 E/AndroidRuntime(17373)        at android.content.ContentResolver.delete(ContentResolver.java  688)            
 E/AndroidRuntime(17373)        at com.android.camera.VideoCamera.deleteCurrentVideo(VideoCamera.java   1090)           
 E/AndroidRuntime(17373)        at com.android.camera.VideoCamera.onClick(VideoCamera.java  497)            
 E/AndroidRuntime(17373)        at android.view.View.performClick(View.java 2485)           
 E/AndroidRuntime(17373)        at android.view.View$PerformClick.run(View.java 9080)           
 E/AndroidRuntime(17373)        at android.os.Handler.handleCallback(Handler.java   587)            
 E/AndroidRuntime(17373)        at android.os.Handler.dispatchMessage(Handler.java  92)         
 E/AndroidRuntime(17373)        at android.os.Looper.loop(Looper.java   130)            
 E/AndroidRuntime(17373)        at android.app.ActivityThread.main(ActivityThread.java  3683)           
 E/AndroidRuntime(17373)        at java.lang.reflect.Method.invokeNative(Native Method)             
 E/AndroidRuntime(17373)        at java.lang.reflect.Method.invoke(Method.java  507)            
 E/AndroidRuntime(17373)        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java   839)            
 E/AndroidRuntime(17373)        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java  597)            
 E/AndroidRuntime(17373)        at dalvik.system.NativeStart.main(Native Method)                

Originally posted 2013-11-16 20:53:28.