0

我在我的相机应用程序中使用内置的 android 意图进行视频录制。我的应用程序可以启动相机应用程序并录制视频,但是当我单击内置相机应用程序的停止按钮时,我的应用程序崩溃并且当检查我保存视频的目录时,录制的视频存储在该目录中。

这是我的代码,请检查。

Button makeVideo = (Button) findViewById(R.id.makeVideo );
            makeVideo.setOnClickListener(new OnClickListener() 
            {
                public void onClick(View v) 
                {

                    Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);

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

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

                    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

                    startActivityForResult(intent, REQUEST_VIDEO_CAPTURED);

                }
            });




    /** Create a file Uri for saving an image or video */
     private static Uri getOutputMediaFileUri() 
     {
         return Uri.fromFile(getOutputMediaFile());
     }

     /** Create a File for saving an image or video */
     private static File getOutputMediaFile() 
     {
         // To be safe, you should check that the SDCard is mounted
         // using Environment.getExternalStorageState() before doing this.

         File mediaStorageDir = new File(Environment.getExternalStorageDirectory().getPath(), "My Videos");
         // 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("MyCameraApp", "failed to create directory");
                 return null;
             }
         }

         // Create a media file name
         String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
         File mediaFile;
         mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "VID_" + timeStamp + ".mp4");
         return mediaFile;
     }


@Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);

            if (resultCode == RESULT_OK)
            {
                if (requestCode == REQUEST_VIDEO_CAPTURED) 
                {
                    uriVideo = data.getData();

                }
            }
    }

这是我的日志猫在此处输入图像描述

4

2 回答 2

0

API 无法处理您提供的路径

mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "VID_" + timeStamp + ".mp4");

尝试:

mediaFile = new File(mediaStorageDir.getPath() + "VID_" + timeStamp + ".mp4");

也许它会解决它或做一个更简单的时间格式:

mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "VID_temp.mp4");
于 2013-10-26T08:12:54.963 回答
0
mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "VID_" + timeStamp + ".mp4");

File.sepator是系统依赖的。File 有一个构造函数,它接受两个参数,一个 File 和一个 String:

mediaFile = new File(mediaStorageDir,  "VID_" + timeStamp + ".mp4");
于 2013-10-26T08:15:12.723 回答