2013-10-23 28 views
0
Android code to keep recording a video and should save only last 10minutes of the video in the sd card? 

我的代碼被記錄並保存在SD卡,但剛過相機被關閉,我得到一個錯誤的內部視頻強制關閉消息。當我使用camera.open(),camera.release()和camera.unlock()方法時,我收到一個未定義的錯誤。我的代碼如下: package com.example.myapp2;Android的代碼,以錄製視頻,只能保存最後視頻的10分鐘在SD卡

import java.io.File; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Locale; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.graphics.Camera; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.Toast; 
import android.widget.VideoView; 

public class MainActivity extends Activity { 
    // Activity request codes 
    private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200; 
    public static final int MEDIA_TYPE_VIDEO = 2; 
    // directory name to store captured images and videos 
    private static final String VIDEO_DIRECTORY_NAME = "Hello Camera"; 
    private Uri fileUri; // file url to store image/video 
    // VideoView videoPreview; 
    Button preview,buttonRecordVideo; 
    Camera camera; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     buttonRecordVideo = (Button) findViewById(R.id.btnRecordVideo); 
     /** 
     * Record video button click event 
     */   
     buttonRecordVideo.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // record video 
       recordVideo(); 
      } 
     }); 
     // Checking camera availability 
     if (!isDeviceSupportCamera()) { 
      Toast.makeText(getApplicationContext(), 
        "Sorry! Your device doesn't support camera", 
        Toast.LENGTH_LONG).show(); 
      // will close the app if the device does't have camera 
      finish(); 
     } 
    } 

    /* 
    * Checking device has camera hardware or not 
    */ 
    private boolean isDeviceSupportCamera() { 
     if (getApplicationContext().getPackageManager().hasSystemFeature(
       PackageManager.FEATURE_CAMERA)) { 
      // this device has a camera 
      return true; 
     } else { 
      // no camera on this device 
      return false; 
     } 
    } 

    /** 
    * Here we store the file url as it will be null after returning from camera 
    * app 
    */ 
    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 

     // save file url in bundle as it will be null on scren orientation 
     // changes 
     outState.putParcelable("file_uri", fileUri); 
    } 

    @Override 
    protected void onRestoreInstanceState(Bundle savedInstanceState) { 
     super.onRestoreInstanceState(savedInstanceState); 

     // get the file url 
     fileUri = savedInstanceState.getParcelable("file_uri"); 
    } 

    /** 
    * Recording video 
    */ 
    private void recordVideo() { 
     Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); 

     fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); 

     // set video quality 
     intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 

     intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file 
                  // name 
     intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 6); //restricting video by time limit(seconds) 
     // intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT,1317961L); //restricting video by using memory size(BYTES) 
     // start the video capture Intent 
     startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE); 
    } 

    /** 
    * Receiving activity result method will be called after closing the camera 
    * */ 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // if the result is capturing Image 
     if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 
       // video successfully recorded 
       // preview the recorded video 
       release(); 
       // previewVideo(); 
      } else if (resultCode == RESULT_CANCELED) { 
       // user cancelled recording 
       Toast.makeText(getApplicationContext(), 
         "User cancelled video recording", Toast.LENGTH_SHORT) 
         .show(); 

      } else { 
       // failed to record video 
       Toast.makeText(getApplicationContext(), 
         "Sorry! Failed to record video", Toast.LENGTH_SHORT) 
         .show(); 
      } 
     } 
    } 

    public final void release() 
    { 
     camera.save(); 
    } 


    /** 
    * ------------ Helper Methods ---------------------- 
    * */ 

    /** 
    * Creating file uri to store image/video 
    */ 
    public Uri getOutputMediaFileUri(int type) { 
     return Uri.fromFile(getOutputMediaFile(type)); 
    } 

    /** 
    * returning image/video 
    */ 
    private static File getOutputMediaFile(int type) { 

     // External sdcard location 
     File mediaStorageDir = new File(
       Environment 
         .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), 
       VIDEO_DIRECTORY_NAME); 

     // Create the storage directory if it does not exist 
     if (!mediaStorageDir.exists()) { 
      if (!mediaStorageDir.mkdirs()) { 
       Log.d(VIDEO_DIRECTORY_NAME, "Oops! Failed create " 
         + VIDEO_DIRECTORY_NAME + " directory"); 
       return null; 
      } 
     } 

     // Create a media file name 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", 
       Locale.getDefault()).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; 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

} 
+1

張貼日誌請.... – AndroidDev

+0

10-23 09:29:06.398:E/AndroidRuntime(7575):致命異常:主 10-23 09:29:06.398:E/AndroidRuntime(7575) :java.lang.NullPointerException 10-23 09:29:06.398:E/AndroidRuntime(7575):\t at java.io.File.fixSlashes(File.java:205) – user2910411

回答

0

在代碼中,你已經聲明瞭一個類級別的變量Camera camera但你是不是在所有使用它的任何地方,事實上,你甚至還沒有初始化,所以它保持空,整個代碼,因此異常。

你在做什麼是使用意圖來記錄視頻。如果您想創建自定義錄像機,請選擇this documentation here by developers.android.com。它有一個很好的和詳細的解決方案,您的問題(代碼

如果這可以解決您的問題,將此標記爲接受的答案。 :)