3

在我的应用程序中,我具有从相机录制视频的功能。该代码适用于所有设备,但使用 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);
    }
4

1 回答 1

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;
    }

希望这会帮助你。

于 2013-11-08T09:13:28.730 回答