2015-05-23 109 views
1

我看过了其他人遇到这个问题并没有找到适当的解决方案。 像他们一样,我遵循相机功能教程:http://developer.android.com/guide/topics/media/camera.html。 下面列出的所有内容都完美地适用于我认为该节目已录制视频的点,正如我所预期的那样。但是,在回顾画廊中的视频时,它并未出现。我很困惑,因为在连接USB调试时没有出现IOException或其他错误。奇怪的是,在移除USB并重新插入时,无论何时何地,无论是立即还是将来的某一天,所有先前录制的视频都会出现在相册中。很明显,我错过了一些我不知道的录制视频的方面。将不胜感激任何帮助或指导,谢谢。MediaRecorder没有保存文件

相关代码如下,如果有人需要,我会发布更多。

摄像头活动:

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

    mCamera = MainActivity.getCameraInstance(); 
    // Create our Preview view and set it as the content of our activity. 
    mPreview = new CameraPreview(this, mCamera); 
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); 
    preview.addView(mPreview); 
    button_capture = (Button) findViewById(R.id.button_capture); 
    button_capture.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if (isRecording) { 
         // stop recording and release camera 
         mMediaRecorder.stop(); // stop the recording 
         releaseMediaRecorder(); // release the MediaRecorder object 
         mCamera.lock();   // take camera access back from MediaRecorder 

         // inform the user that recording has stopped 
         button_capture.setText("Capture"); 
         isRecording = false; 
        } else { 
         // initialize video camera 
         if (prepareVideoRecorder()) { 
          // Camera is available and unlocked, MediaRecorder is prepared, 
          // now you can start recording 
          mMediaRecorder.start(); 

          // inform the user that recording has started 
          button_capture.setText("Stop"); 
          isRecording = true; 
         } else { 
          // prepare didn't work, release the camera 
          releaseMediaRecorder(); 
          // inform user 
         } 
        } 
       } 
      }); 
} 

private boolean prepareVideoRecorder(){ 

    mMediaRecorder = new MediaRecorder(); 

    // Step 1: Unlock and set camera to MediaRecorder 
    mCamera.unlock(); 
    mMediaRecorder.setCamera(mCamera); 

    // Step 2: Set sources 
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); 
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 

    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher) 
    mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH)); 

    // Step 4: Set output file 
    mMediaRecorder.setOutputFile(MediaCapture.getOutputMediaFile(MEDIA_TYPE_VIDEO).toString()); 

    // Step 5: Set the preview output 
    mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface()); 

    // Step 6: Prepare configured MediaRecorder 
    try { 
     mMediaRecorder.prepare(); 
    } catch (IllegalStateException e) { 
     Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage()); 
     releaseMediaRecorder(); 
     return false; 
    } catch (IOException e) { 
     Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage()); 
     releaseMediaRecorder(); 
     return false; 
    } 
    return true; 
} 

媒体捕获:

public static Uri getOutputMediaFileUri(int type) { 
    return Uri.fromFile(getOutputMediaFile(MEDIA_TYPE_VIDEO)); 
} 

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

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_MOVIES), "MyApplication"); 

    // 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("MyApplication", "failed to create directory"); 
      return null; 
     } 
    } 

     // Create a media file name 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HH:mm:ss").format(new Date()); 
     File mediaFile; 
     if (type == MEDIA_TYPE_VIDEO) { 
      mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
        "VID_" + timeStamp + ".mp4"); 
     } else { 
      return null; 
     } 
     return mediaFile; 
    } 
} 

回答

0

我也很是通过这种行为,直到我意识到拔下USB是造成文件出现困惑。这似乎是Android的一个bug,可能与此相关的一个:

https://code.google.com/p/android/issues/detail?id=195362

显然,该文件已被正确地写入,但由于某些原因,新的文件是不可见的。无论如何,上面链接中提供的解决方案/解决方法是强制对您的文件进行媒体扫描。例如

MediaScannerConnection.scanFile(getContext(), new String[]{this.mediaFile.getAbsolutePath()}, null, null); 

其中'mediaFile'是mediaRecorder刚完成写入的文件。有了这个,我不需要拔下USB电缆来查看该文件,并且在录制完成后立即出现。

我在Samsung Galaxy A5上运行Android 5.0.2。这感觉比解决方案更多的解决方法,我不能确定它会在其他设备或Android版本上工作,但我希望它可以帮助某人。