Android视频捕获意图获取错误?

时间:2014-04-14 07:03:28

标签: android android-camera android-video-player

我是Android新手。 我想使用相机进行视频录制,我的视频正在录制,并保存在我创建的文件夹中。但还有另一个问题。当我停止视频时 这显示了一个拨号,包含此消息。

"对不起" 应用程序相机(Process com.sec.android.app.camera)意外停止。请再试一次。

和一个强制关闭的按钮。

任何人都可以告诉我,问题是什么,我缺少什么。我的代码在这里。

public class MainActivity extends Activity {

private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;
public static final int MEDIA_TYPE_VIDEO = 2;

// directory name to store captured images and videos
private static final String IMAGE_DIRECTORY_NAME = "Hello Camera";

private Uri fileUri; // file url to store image/video

private VideoView videoPreview;
private Button btnCapturePicture, btnRecordVideo;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    videoPreview = (VideoView) findViewById(R.id.videoPreview);
    btnRecordVideo = (Button) findViewById(R.id.btnRecordVideo);

    /**
     * Record video button click event
     */
    btnRecordVideo.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // record video
            recordVideo();
        }
    });
 // Checking camera availability
        if (!isDeviceSupportCamera()) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! Your device doesn't support camera",
                    Toast.LENGTH_LONG).show();
            // will close the app if the device does't have camera
            finish();
        }
}

/**
 * Checking device has camera hardware or not
 * */
private boolean isDeviceSupportCamera() {
    if (getApplicationContext().getPackageManager().hasSystemFeature(
            PackageManager.FEATURE_CAMERA)) {
        // this device has a camera
        return true;
    } else {
        // no camera on this device
        return false;
    }
}
/*
 * Recording video
 */
private void recordVideo() {
    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);

    // set video quality
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
                                                        // name

    // start the video capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
}

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



/**
 * Receiving activity result method will be called after closing the camera
 * */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if the result is capturing Image
    if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // video successfully recorded
            // preview the recorded video
            previewVideo();

        } else if (resultCode == RESULT_CANCELED) {
            // user cancelled recording
            Toast.makeText(getApplicationContext(),
                    "User cancelled video recording", Toast.LENGTH_SHORT)
                    .show();
        } else {
            // failed to record video
            Toast.makeText(getApplicationContext(),
                    "Sorry! Failed to record video", Toast.LENGTH_SHORT)
                    .show();
        }
    }

}



/*
 * Previewing recorded video
 */
private void previewVideo() {
    try {
        // hide image preview
        imgPreview.setVisibility(View.GONE);

        videoPreview.setVisibility(View.VISIBLE);
        videoPreview.setVideoPath(fileUri.getPath());
        // start playing
        videoPreview.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * ------------ Helper Methods ---------------------- 
 * */

/*
 * 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),IMAGE_DIRECTORY_NAME);

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

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    File mediaFile;
     (type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

}

日志文件是这个

  2014-04-14 11:17:21.863 E 2873/AndroidRuntime: FATAL EXCEPTION: main
  java.lang.NullPointerException
  at java.io.File.fixSlashes(File.java:205)
  at java.io.File.init(File.java:189)
  at java.io.File.<init>(File.java:139)
  at com.sec.android.app.camera.CamcorderEngine.renameTempFile(CamcorderEngine.java:1158)
  at   com.sec.android.app.camera.CamcorderEngine.doStopVideoRecordingSync(CamcorderEngine.java:78     5)
  at com.sec.android.app.camera.CeStateRecording.handleRequest(CeStateRecording.java:73)
 at com.sec.android.app.camera.CeRequestQueue.startFirstRequest(CeRequestQueue.java:118)
 at com.sec.android.app.camera.CeRequestQueue.access$300(CeRequestQueue.java:32)
 at com.sec.android.app.camera.CeRequestQueue$MainHandler.handleMessage(CeRequestQueue.java:57)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:130)
 at android.app.ActivityThread.main(ActivityThread.java:3687)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:507)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
 at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案