来自使用nexus 7的摄像头的Android视频记录返回空数据

时间:2013-11-08 07:56:08

标签: android android-intent nexus-7

在我的应用中,我有从相机录制视频的功能。代码适用于所有设备,但与nexus-7(Android OS 4.3)我得到null  onActivityResult(int requestCode, int resultCode, Intent data)方法。

当我调试我的代码时,我注意到nexus-7(android OS 4.3)的Intent数据为空

请为此提出一些解决方案。

这里是recordVideo方法的代码

  private void recordVideo() {
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);


        // set video quality
        // 1- for high quality video
        Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);


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

1 个答案:

答案 0 :(得分:1)

请修改您的recordView方法,如下所示: -

private void recordVideo() {
intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);    
   fileUri = getOutputMediaFile(MEDIA_TYPE_VIDEO);  // create a file to save the video in specific folder (this works for video only)
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high

    // start the Video Capture Intent
    startActivityForResult(intent, REQUEST_VIDEO_CAPTURED_NEXUS);

 }

//然后在onActivityResult方法

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

            if (resultCode == Activity.RESULT_OK) {
                switch (requestCode) {
        case REQUEST_VIDEO_CAPTURED_NEXUS:
        this.videoFromCameraNexus(resultCode, data);
        break;

    default:
                    break;
                }


            }
        }

// videoFromCameraNexus方法

private void videoFromCameraNexus(int resultCode, Intent data) {

        if(fileUri != null) {
            Log.d(TAG, "Video saved to:\n" + fileUri);
            Log.d(TAG, "Video path:\n" + fileUri.getPath());
            Log.d(TAG, "Video name:\n" + getName(fileUri)); 
    // use uri.getLastPathSegment() if store in folder
    //use the file Uri.
        }
    }
Get the output Media file uri with the following Method

public Uri getOutputMediaFile(int type)
    {
        // To be safe, you should check that the SDCard is mounted

        if(Environment.getExternalStorageState() != null) {
            // this works for Android 2.2 and above
            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), "SMW_VIDEO");

            // 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(TAG, "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_VIDEO) {
                mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "VID_"+ timeStamp + ".mp4");
            } else {
                return null;
            }

            return Uri.fromFile(mediaFile);
        }

        return null;
    }

希望这会对你有所帮助。