保存视频时相机崩溃

时间:2012-11-14 12:31:14

标签: android android-camera

我正在处理一个应用程序,当用户按下某个按钮时,我想打开相机来录制视频。

这是启动相机的方法:

public void recordVideo(boolean highQuality) {
    if (isIntentAvailable(MediaStore.ACTION_VIDEO_CAPTURE)) {

        videoFile = createFile(MEDIA_TYPE_VIDEO);

        if(videoFile != null){
            videoFileUri = Uri.fromFile(videoFile);

            Intent recordVideo = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

            recordVideo.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                    videoFileUri);
            recordVideo.putExtra(
                    android.provider.MediaStore.EXTRA_VIDEO_QUALITY,
                    highQuality ? 1 : 0);
            recordVideo.putExtra(
                    android.provider.MediaStore.EXTRA_DURATION_LIMIT, 30);

            cameraStarted = true;
            startActivityForResult(recordVideo, ACTION_TAKE_VIDEO);
        }
    } else {
        Toast.makeText(this, R.string.no_camera, Toast.LENGTH_SHORT).show();
    }
}

我在这里制作文件:

private File createFile(int type) {
    File mediaFile = null;

    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

        File mediaStorageDir = new File(
                Environment.getExternalStorageDirectory() + "/Zorgdossier", "Media");
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d(TAG, "failed to create directory");
                return null;
            }
        }

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());

        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + "/" + "IMG_"
                    + timeStamp + ".jpg");
        } else if (type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + "/" + "VID_"
                    + timeStamp + ".mp4");
        } else {
            return null;
        }
    }
    else{
        Log.d(TAG, "No external storage found");
        Toast.makeText(this, getString(R.string.storageNotAvailable), Toast.LENGTH_SHORT).show();
    }

    return mediaFile;
}

当我录制视频并按下保存按钮时,应用程序崩溃,我收到以下错误:

这是错误提示:

E/AndroidRuntime( 3972): FATAL EXCEPTION: main
E/AndroidRuntime( 3972): java.lang.NullPointerException
E/AndroidRuntime( 3972):    at java.io.File.fixSlashes(File.java:205)
E/AndroidRuntime( 3972):    at java.io.File.init(File.java:189)
E/AndroidRuntime( 3972):    at java.io.File.<init>(File.java:139)
E/AndroidRuntime( 3972):    at com.sec.android.app.camera.CamcorderEngine.renameTempFile(CamcorderEngine.java:1132)
E/AndroidRuntime( 3972):    at com.sec.android.app.camera.CamcorderEngine.doStopVideoRecordingSync(CamcorderEngine.java:772)
E/AndroidRuntime( 3972):    at com.sec.android.app.camera.CeStateRecording.handleRequest(CeStateRecording.java:69)
E/AndroidRuntime( 3972):    at com.sec.android.app.camera.CeRequestQueue.startFirstRequest(CeRequestQueue.java:104)
E/AndroidRuntime( 3972):    at com.sec.android.app.camera.CeRequestQueue.access$300(CeRequestQueue.java:32)
E/AndroidRuntime( 3972):    at com.sec.android.app.camera.CeRequestQueue$MainHandler.handleMessage(CeRequestQueue.java:54)
E/AndroidRuntime( 3972):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 3972):    at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 3972):    at android.app.ActivityThread.main(ActivityThread.java:3687)
E/AndroidRuntime( 3972):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 3972):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 3972):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
E/AndroidRuntime( 3972):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
E/AndroidRuntime( 3972):    at dalvik.system.NativeStart.main(Native Method)

任何想法导致此错误的原因是什么? 奇怪的是,录制的视频正确保存在我指定的地图中。

编辑:这是在三星Galaxy gio上测试的。

2 个答案:

答案 0 :(得分:0)

你的方式有问题!?

看看这个previous answer

answer to underrating the step

答案 1 :(得分:0)

我找到了答案here,显然MediaStore.EXTRA_OUTPUT无法正常工作。