我正在尝试使用默认相机应用程序使用意图处理记录/拍照,但似乎无法让默认代码正常工作。http://developer.android.com/guide/topics/media/camera.html
此代码适用于冰淇淋三明治,但适用于 Gingerbread (2.3) 尝试录制视频时,图像/预览被冻结,当我尝试重新拍摄视频时,它崩溃。
从堆栈跟踪来看,它似乎确实支持视频格式,但它在文档中明确支持它......
任何帮助或想法将不胜感激。:)
我的代码:
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;
}
}
堆栈跟踪:
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)