java.lang.RuntimeException:传递结果失败ResultInfo {who = null,request = 200,result = 0,data = null} java.lang.NullPointerException

时间:2014-11-07 05:12:16

标签: android

我的应用程序允许用户按下按钮,它打开摄像头进行视频捕获,他们可以拍摄视频,它将显示在SD卡中。如果用户在相机打开时按下停止,我会关闭此强制 - 将结果ResultInfo {who = null,request = 200,result = 0,data = null}发送到活动失败。 每当我停止录制视频时,应用程序都会崩溃。但是视频存储在目录中,并使用相同的文件命名格式。

这是活动类

public class VideocameraActivity extends Activity {

private Uri fileUri;
public static final int MEDIA_TYPE_VIDEO = 2;
private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
public static VideocameraActivity ActivityContext =null; 
public static TextView output;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.observation);

    ActivityContext = this;

    ImageView buttonRecording = (ImageView)findViewById(R.id.add_rowBtn);
    //output = (TextView)findViewById(R.id.output);

    buttonRecording.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View arg0) {

            // create new Intentwith with Standard Intent action that can be
            // sent to have the camera application capture an video and return it. 
            Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

            // create a file to save the video
            fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); 

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

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

            // start the Video Capture Intent
            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){

    // Check that the SDCard is mounted
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "MyCameraVideo");


    // Create the storage directory(MyCameraVideo) if it does not exist
    if (! mediaStorageDir.exists()){

        if (! mediaStorageDir.mkdirs()){

            output.setText("Failed to create directory MyCameraVideo.");

            Toast.makeText(ActivityContext, "Failed to create directory MyCameraVideo.", 
                    Toast.LENGTH_LONG).show();

            Log.d("MyCameraVideo", "Failed to create directory MyCameraVideo.");
            return null;
        }
    }


    // Create a media file name

    // For unique file name appending current timeStamp with file name
    java.util.Date date= new java.util.Date();
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                         .format(date.getTime());

    File mediaFile;

    if(type == MEDIA_TYPE_VIDEO) {

        // For unique video file name appending current timeStamp with file name
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "VID_"+ timeStamp + ".mp4");

    } else {
        return null;
    }

    return mediaFile;
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    // After camera screen this code will executed

    if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {

        if (resultCode == RESULT_OK) {

            //output.setText("Video File : " +data.getData());

            // Video captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Video saved to:\n" +
                     data.getData(), Toast.LENGTH_LONG).show();

        } /*else if (resultCode == RESULT_CANCELED) {

            output.setText("User cancelled the video capture.");

            // User cancelled the video capture
            Toast.makeText(this, "User cancelled the video capture.", 
                    Toast.LENGTH_LONG).show();

        } else {

            output.setText("Video capture failed.");

            // Video capture failed, advise user
            Toast.makeText(this, "Video capture failed.", 
                    Toast.LENGTH_LONG).show();
        }*/
    }
}

}

Log Cat错误:

11-07 06:48:59.062: D/dalvikvm(1585): GC_FOR_ALLOC freed 71K, 6% free 2869K/3048K, paused 9ms, total 10ms
11-07 06:48:59.074: I/dalvikvm-heap(1585): Grow heap (frag case) to 7.069MB for 4367372-byte allocation
11-07 06:48:59.090: D/dalvikvm(1585): GC_FOR_ALLOC freed <1K, 3% free 7134K/7316K, paused 17ms, total 17ms
11-07 06:48:59.174: D/dalvikvm(1585): GC_FOR_ALLOC freed 1K, 3% free 7610K/7776K, paused 6ms, total 6ms
11-07 06:48:59.206: D/dalvikvm(1585): GC_FOR_ALLOC freed 5K, 2% free 9140K/9300K, paused 2ms, total 2ms
11-07 06:48:59.562: D/libEGL(1585): loaded /system/lib/egl/libEGL_genymotion.so
11-07 06:48:59.562: D/(1585): HostConnection::get() New Host Connection established 0xb95313a8, tid 1585
11-07 06:48:59.570: D/libEGL(1585): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
11-07 06:48:59.570: D/libEGL(1585): loaded /system/lib/egl/libGLESv2_genymotion.so
11-07 06:48:59.606: W/EGL_genymotion(1585): eglSurfaceAttrib not implemented
11-07 06:48:59.610: E/OpenGLRenderer(1585): Getting MAX_TEXTURE_SIZE from GradienCache
11-07 06:48:59.610: E/OpenGLRenderer(1585): MAX_TEXTURE_SIZE: 8192
11-07 06:48:59.614: E/OpenGLRenderer(1585): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
11-07 06:48:59.614: E/OpenGLRenderer(1585): MAX_TEXTURE_SIZE: 8192
11-07 06:48:59.614: D/OpenGLRenderer(1585): Enabling debug mode 0
11-07 06:49:14.386: W/EGL_genymotion(1585): eglSurfaceAttrib not implemented
11-07 06:49:34.218: D/dalvikvm(1585): GC_FOR_ALLOC freed 645K, 7% free 10544K/11296K, paused 9ms, total 19ms
11-07 06:49:38.050: W/EGL_genymotion(1585): eglSurfaceAttrib not implemented
11-07 06:49:38.614: I/System.out(1585): Url--------login--> http://ilearniteasy.com/new-version/api/get_login.php?salt=8006&sign=85db4eebea8fca6814ad73a75efcdbf5&site_url=http://www.mydistance-learning-college.com/students/&username=teststudent7&password=password1
11-07 06:49:38.730: W/EGL_genymotion(1585): eglSurfaceAttrib not implemented
11-07 06:49:40.362: I/Choreographer(1585): Skipped 97 frames!  The application may be doing too much work on its main thread.
11-07 06:49:44.230: D/dalvikvm(1585): GC_FOR_ALLOC freed 240K, 5% free 12079K/12676K, paused 3ms, total 15ms
11-07 06:49:44.278: W/EGL_genymotion(1585): eglSurfaceAttrib not implemented
11-07 06:49:59.114: W/EGL_genymotion(1585): eglSurfaceAttrib not implemented
11-07 06:50:02.946: W/EGL_genymotion(1585): eglSurfaceAttrib not implemented

1 个答案:

答案 0 :(得分:0)

请检查

if(data!=null) {

...

}