title: This is my code for recording video directly in Android,but it doesn't work.It shows a black color screen only.Anyone can help me please?
body: <p>This is my code for recording video directly in Android. But it doesn't work, it shows only a black screen when running.</p>
  
  <p>Permissions are:
   Write to external storage,Record Audio and Camera</p>
  
  <p>MainActivity:</p>
  
  <pre><code>import java.io.File;
  import java.io.IOException;
  import java.util.Date;
  import android.app.Activity;
  import android.hardware.Camera;
  import android.hardware.Camera.Parameters;
  import android.media.MediaRecorder;
  import android.os.Bundle;
  import android.os.Environment;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.SurfaceHolder;
  import android.view.SurfaceHolder.Callback;
  import android.view.SurfaceView;
  import android.widget.Toast;
  
  public class MainActivity extends Activity implements Callback {
  
   @Override
   protected void onDestroy() {
      stopRecording();
      super.onDestroy();
   }
  
  private SurfaceHolder surfaceHolder;
  private SurfaceView surfaceView;
  public MediaRecorder mrec = new MediaRecorder();
  private Camera mCamera;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      surfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
      mCamera = Camera.open();
  
      surfaceHolder = surfaceView.getHolder();
      surfaceHolder.addCallback(this);
      surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
  
      menu.add(0, 0, 0, "Start");
      return super.onCreateOptionsMenu(menu);
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      if (item.getTitle().equals("Start")) {
          try {
  
              startRecording();
              item.setTitle("Stop");
  
          } catch (Exception e) {
  
              String message = e.getMessage();
              Log.i(null, "Problem " + message);
              mrec.release();
          }
  
      } else if (item.getTitle().equals("Stop")) {
          mrec.stop();
          mrec.release();
          mrec = null;
          item.setTitle("Start");
      }
  
      return super.onOptionsItemSelected(item);
  }
  
  protected void startRecording() throws IOException {
      if (mCamera == null)
          mCamera = Camera.open();
  
      String filename;
      String path;
  
      path = Environment.getExternalStorageDirectory().getAbsolutePath()
              .toString();
  
      Date date = new Date();
      filename = "/rec" + date.toString().replace(" ", "_").replace(":", "_")
              + ".mp4";
  
      // create empty file it must use
      File file = new File(path, filename);
  
      mrec = new MediaRecorder();
  
      mCamera.lock();
      mCamera.unlock();
  
      // Please maintain sequence of following code.
  
      // If you change sequence it will not work.
      mrec.setCamera(mCamera);
      mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
      mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
      mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
      mrec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
      mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
      mrec.setPreviewDisplay(surfaceHolder.getSurface());
      mrec.setOutputFile(path + filename);
      mrec.prepare();
      mrec.start();
  
     }
  
    protected void stopRecording() {
  
      if (mrec != null) {
          mrec.stop();
          mrec.release();
          mCamera.release();
          mCamera.lock();
      }
     }
  
  private void releaseMediaRecorder() {
  
      if (mrec != null) {
          mrec.reset(); // clear recorder configuration
          mrec.release(); // release the recorder object
      }
    }
  
  
  
  
  
  
  enter code here
  
  private void releaseCamera() {
      if (mCamera != null) {
          mCamera.release(); // release the camera for other applications
          mCamera = null;
      }
  
  
  
     }
  
  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
          int height) {
  
  }
  
  @Override
  public void surfaceCreated(SurfaceHolder holder) {
  
      if (mCamera != null) {
          Parameters params = mCamera.getParameters();
          mCamera.setParameters(params);
          Log.i("Surface", "Created");
      } else {
          Toast.makeText(getApplicationContext(), "Camera not available!",
                  Toast.LENGTH_LONG).show();
  
          finish();
      }
  
  
      }
  
  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
      mCamera.stopPreview();
      mCamera.release();
  
  }
  }
  </code></pre>
  
  <p>This is my xml code</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"`enter code here`
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity" &gt;
  
  &lt;SurfaceView
      android:id="@+id/surfaceView1"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_centerInParent="true"
      android:layout_weight="1" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>Here in my Android Manifest file</p>
  
  
  
  <p>
  
  <pre><code>package="com.example.videorecord"
  
  android:versionCode="1"
  
  android:versionName="1.0" &gt;
  
  
  
  &lt;uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="18" /&gt;
  
  &lt;uses-permission android:name="android.permission.CAMERA"/&gt;
  
  &lt;uses-permission android:name="android.permission.RECORD_AUDIO"/&gt;
  
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;
  
  
  &lt;application 
  
      android : allowBackup="true"
  
      android:icon=  "@drawable /ic_launcher"
  
      android:label="@string/app_name"
  
      android:theme="@style/AppTheme" &gt;
  
      &lt;activity
          android:name="com.example.videorecord.MainActivity"
  
          android:label="@string/app_name" &gt;
  
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
  
          &lt;/intent-filter&gt;
  
      &lt;/activity&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p></p>