2013-10-26 32 views
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(); 

       } 
      } 
    } 

這裏是我的logcat enter image description here

+0

你在彙編中有write_external_storage權限嗎? – Blackbelt

+0

他應該有,因爲沒有安全例外 – 2013-10-26 08:13:20

+0

嘗試此鏈接 [你的是這可能重複] [1] [1]:http://stackoverflow.com/questions/7846772/mysterious -nullpointerexception-after-the-built-in-camera-app-saves-my-video-pro?rq = 1 –

回答

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"); 
+0

仍然顯示相同的錯誤 – user2391890

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

File.sepator系統依賴。文件有一個構造函數,它需要兩個參數,一個文件和一個字符串:

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

仍顯示相同的錯誤 – user2391890

相關問題