0

我想从我的活动中捕捉视频

这是我的源代码

mTempVideoPath = Environment.getExternalStorageDirectory().getName() + File.separatorChar + "Android/data/" + this.getPackageName() + "/files/" + System.currentTimeMillis() + ".mp4";

File videoFile = new File(mTempVideoPath);
                            try {
                                if(videoFile.exists() == false) {
                                    videoFile.getParentFile().mkdirs();
                                    videoFile.createNewFile();
                                }

                            } catch (IOException e) {
                                Log.e(getCallingPackage(), "Could not create file.", e);
                            }
                            mCurrentVideoURI = Uri.fromFile(videoFile);

                            Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE );
                            intent.putExtra( MediaStore.EXTRA_OUTPUT, mCurrentVideoURI);
                            // preventing it from listing items that aren't on the SD card
                            intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
                            intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
                            startActivityForResult(intent, CAPTURE_VIDEO);

但我得到这个错误:

09-17 18:25:28.945: E/AndroidRuntime(30253): FATAL EXCEPTION: main
09-17 18:25:28.945: E/AndroidRuntime(30253): java.lang.NullPointerException
09-17 18:25:28.945: E/AndroidRuntime(30253):    at java.io.File.fixSlashes(File.java:185)
09-17 18:25:28.945: E/AndroidRuntime(30253):    at java.io.File.<init>(File.java:134)
09-17 18:25:28.945: E/AndroidRuntime(30253):    at com.sec.android.app.camera.CamcorderEngine.renameTempFile(CamcorderEngine.java:1467)
09-17 18:25:28.945: E/AndroidRuntime(30253):    at com.sec.android.app.camera.CamcorderEngine.doStopVideoRecordingSync(CamcorderEngine.java:965)
09-17 18:25:28.945: E/AndroidRuntime(30253):    at com.sec.android.app.camera.CeStateRecording.handleRequest(CeStateRecording.java:69)
09-17 18:25:28.945: E/AndroidRuntime(30253):    at com.sec.android.app.camera.CeRequestQueue.startFirstRequest(CeRequestQueue.java:123)
09-17 18:25:28.945: E/AndroidRuntime(30253):    at com.sec.android.app.camera.CeRequestQueue.access$200(CeRequestQueue.java:32)
09-17 18:25:28.945: E/AndroidRuntime(30253):    at com.sec.android.app.camera.CeRequestQueue$MainHandler.handleMessage(CeRequestQueue.java:60)
09-17 18:25:28.945: E/AndroidRuntime(30253):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 18:25:28.945: E/AndroidRuntime(30253):    at android.os.Looper.loop(Looper.java:137)
09-17 18:25:28.945: E/AndroidRuntime(30253):    at android.app.ActivityThread.main(ActivityThread.java:4507)
09-17 18:25:28.945: E/AndroidRuntime(30253):    at java.lang.reflect.Method.invokeNative(Native Method)
09-17 18:25:28.945: E/AndroidRuntime(30253):    at java.lang.reflect.Method.invoke(Method.java:511)
09-17 18:25:28.945: E/AndroidRuntime(30253):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
09-17 18:25:28.945: E/AndroidRuntime(30253):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
09-17 18:25:28.945: E/AndroidRuntime(30253):    at dalvik.system.NativeStart.main(Native Method)

我在 Galaxy S2、ICS 上运行!

请帮帮我!

谢谢

4

2 回答 2

2

我在这里找到了很好的解决方案:尝试录制视频时出现 VerifiyAndSetParameter 错误

MediaStore.EXTRA_OUTPUT不能正常工作并且有问题。

于 2012-09-18T02:48:23.480 回答
0

早期版本的 Android(如 2.3)有一个错误,会导致媒体播放器在拍摄电影后崩溃。重命名文件时引发 NullPointer 异常。解决方法是在启动记录器时不提供文件 Uri 并选择默认文件。我不确定该错误何时得到修复,但重命名的 hack 适用于最新版本。

感谢所有帮助,这就是我所做的:

    private static final int VIDEO_REQUEST = 43;    // some action code
    File videoFile = new File(dirName, fileName);
    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(videoFile));
    startActivityForResult(intent, VIDEO_REQUEST);

// Then later...

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    if (resultCode == RESULT_OK) {
        if (requestCode == VIDEO_REQUEST  &&
                Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            try {  // cloodge
                // first get filename used by media recorder
                String[] projection = { MediaStore.Images.Media.DATA }; // columns
                Cursor cursor = managedQuery(intent.getData(), projection, null, null, null);
                int iColumn= cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                String tempFileName = cursor.getString(iColumn);
                File tempFile = new File(tempFileName);
                File videoFile = new File(dirName, fileName);
                if (!tempFile.renameTo(videoFile)) { // try to rename it
                    // rename did not work.  Copy and delete
                    FileInputStream in = new FileInputStream(tempFile);
                    FileOutputStream out = new FileOutputStream(videoFile);

                    byte[] buffer = new byte[1024]; // copy file to where we want it
                    int length;
                    while ((length = in.read(buffer)) > 0)
                        out.write(buffer, 0, length);
                    in.close();
                    out.close();

                    // now try to delete file created by the media recorder
                    tempFile.delete(); 
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } // end of painful cloodge
        // video file is in videoFile, like it should be
        ...
    }
}
于 2013-08-10T21:38:43.133 回答