After following above program i still have tons of error on my list what step i could have missed... I really need to get it to work before i could start on my project...

http://integratingstuff.com/2010/10/18/writing-code-that-captures-videos-on-android/

Because i doing an android application similar to this apps http://www.androidzoom.com/android_applications/travel_and_local/traffic-recorder_remv_screenshots.html
but it seems like the above code i copied in this website http://integratingstuff.com/2010/10/18/writing-code-that-captures-videos-on-android/ does not seems to allow me record any videos and saving it into an SD Card location? What should i do to get the video recorded and saved into a sd card.. Can someone help me i'm actually new in Eclipse and java programming so pardon me for any stupid thing i do in programming..

package net.learn2develop.CameraTest;

import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

public class CameraTest extends Activity implements SurfaceHolder.Callback {
	
	private static final String TAG ="CAMERA_TUTORIAL";
	
	private SurfaceView surfaceView;
	private SurfaceHolder surfaceHolder;
	private Camera camera;
	private boolean previewRunning;

	File tempFile = null;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        surfaceView = (SurfaceView)findViewById(R.id.surface_camera);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    	camera = Camera.open();
    	if (camera != null) {
    		Camera.Parameters params = camera.getParameters();
    		camera.setParameters(params);
    	}
    	else {
    		Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
    		finish();
    	}
    }
    
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    	if (previewRunning) {
    		camera.stopPreview();
    	}
    	Camera.Parameters p = camera.getParameters();
    	p.setPreviewSize(width, height);
    	p.setPreviewFormat(PixelFormat.JPEG);
    	//camera.setParameters(p);
    	
    	try {
    			camera.setPreviewDisplay(holder);
    			camera.startPreview();
    			previewRunning = true;
    	}
    	catch (IOException e) {
    		Log.e(TAG,e.getMessage());
    		e.printStackTrace();
    	}
    }
    
    @Override
    public void surfaceDestroyed(SurfaceHolder holder){
    		camera.stopPreview();
    		previewRunning = false;
    		camera.release();
    }

How am i going to allow background video recording after i leave the application,or received an sms/call and when i'm come back its still recording and data was not lost because after all i'm creating an car blackbox android device??

public class CameraTest extends Activity implements SurfaceHolder.Callback {
	
	private static final String TAG ="CAMERA_TUTORIAL";
	
	private SurfaceView surfaceView;
	private SurfaceHolder surfaceHolder;
	private Camera camera;
	private boolean previewRunning;

	File tempFile = null;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        surfaceView = (SurfaceView)findViewById(R.id.surface_camera);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        
        Button btnStart = (Button) findViewById(R.id.button4);
        btnStart.setOnClickListener(new View.OnClickListener() 
        {
        	public void onClick(View v)
        	{
        		startRecording();
        	}
        	
        });
        
        Button btnStop = (Button) findViewById(R.id.button5);
        btnStop.setOnClickListener(new View.OnClickListener() 
        {
        	public void onClick(View v)
        	{
        		stopRecording();
        	}
        	
        });
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    	camera = Camera.open();
    	if (camera != null) {
    		Camera.Parameters params = camera.getParameters();
    		camera.setParameters(params);
    	}
    	else {
    		Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
    		finish();
    	}
    }
    
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    	if (previewRunning) {
    		camera.stopPreview();
    	}
    	Camera.Parameters p = camera.getParameters();
    	p.setPreviewSize(320, 240);
    	p.setPreviewFormat(PixelFormat.JPEG);
    	camera.setParameters(p);
    	
    	try {
    			camera.setPreviewDisplay(holder);
    			camera.startPreview();
    			previewRunning = true;
    	}
    	catch (IOException e) {
    		Log.e(TAG,e.getMessage());
    		e.printStackTrace();
    	}
    }
    
    @Override
    public void surfaceDestroyed(SurfaceHolder holder){
    		camera.stopPreview();
    		previewRunning = false;
    		camera.release();
    }
    
    
    
    private MediaRecorder mediaRecorder;
    private final int maxDurationInMs = 20000;
    private final int videoFramesPerSecond = 20;
    
    public boolean startRecording(){
    	try {
    			camera.unlock();
    			
    			mediaRecorder = new MediaRecorder();
    			
    			mediaRecorder.setCamera(camera);
    			
    			mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    
    			mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    
    			mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
    				
    			mediaRecorder.setMaxDuration(maxDurationInMs);
    				
    				
    			
    				
    				mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    				
    				mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
    				
    				//mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
    				
    				tempFile = new File(Environment.getExternalStorageDirectory(),"1.3gp");
    				mediaRecorder.setOutputFile(tempFile.getPath());
    				
    				mediaRecorder.setVideoSize(surfaceView.getWidth(),surfaceView.getHeight());
    				
    				//mediaRecorder.setVideoFrameRate(videoFramesPerSecond);
    				
    				mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
    				
    								
    						mediaRecorder.prepare();
    						mediaRecorder.start();	
    						return true;
    								
    	} catch (IllegalStateException e) {
    		Log.e(TAG,e.getMessage());
    		e.printStackTrace();
    		return false;
    	} catch (IOException e) {
    		Log.e(TAG,e.getMessage());
    		e.printStackTrace();
    		return false;
    	}
    }
    
    public void stopRecording(){
    	mediaRecorder.stop();
    	camera.lock();
    }
}

I would appreciate if you stop create new threads and stick with this as all your problems & questions are the same issue of how to record video.

oh oh sorry for your inconvenience.. I'm just desperate because there isn't a lot of people answering my question.. And i'm kinna bad in programming..

Hey any kind soul out there willing to help me on how am i going to implement background video recording for my above problem?? Should i be using services to do invoke background recording instead, if so how am i going to do it?? I tried reading lots of site but still i don't know how am i going to d it for my case to allow services to take over in another .java file to continue the recording process...

public class CameraTest extends Activity implements SurfaceHolder.Callback {
   
   private static final String TAG ="CAMERA_TUTORIAL";
   
   private SurfaceView surfaceView;
   private SurfaceHolder surfaceHolder;
   private Camera camera;
   private boolean previewRunning;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        surfaceView = (SurfaceView)findViewById(R.id.surface_camera);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        
        Button btnStart = (Button) findViewById(R.id.button4);
        btnStart.setOnClickListener(new View.OnClickListener() 
        {
           public void onClick(View v)
           {
              startRecording();
           }
           
        });
        
        Button btnStop = (Button) findViewById(R.id.button5);
        btnStop.setOnClickListener(new View.OnClickListener() 
        {
           public void onClick(View v)
           {
              stopRecording();
           }
           
        });
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
       camera = Camera.open();
       if (camera != null) {
          Camera.Parameters params = camera.getParameters();
          camera.setParameters(params);
       }
       else {
          Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
          finish();
       }
    }
    
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
       if (previewRunning) {
          camera.stopPreview();
       }
       Camera.Parameters p = camera.getParameters();
       p.setPreviewSize(320, 240);
       p.setPreviewFormat(PixelFormat.JPEG);
       camera.setParameters(p);
       
       try {
             camera.setPreviewDisplay(holder);
             camera.startPreview();
             previewRunning = true;
       }
       catch (IOException e) {
          Log.e(TAG,e.getMessage());
          e.printStackTrace();
       }
    }
    
    @Override
    public void surfaceDestroyed(SurfaceHolder holder){
          camera.stopPreview();
          previewRunning = false;
          camera.release();
    }
    
    
    
    private MediaRecorder mediaRecorder;
    private final int maxDurationInMs = 20000;
    private final int videoFramesPerSecond = 20;
    
    public boolean startRecording(){
       try {
             camera.unlock();
             
             mediaRecorder = new MediaRecorder();
             
             mediaRecorder.setCamera(camera);
             
             mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    
             mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    
             mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
                
             mediaRecorder.setMaxDuration(maxDurationInMs);
                
                
             
                
                mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
                
                mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
                
                //mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
                
                tempFile = new File(Environment.getExternalStorageDirectory(),"1.3gp");
                mediaRecorder.setOutputFile(tempFile.getPath());
                
                mediaRecorder.setVideoSize(surfaceView.getWidth(),surfaceView.getHeight());
                
                //mediaRecorder.setVideoFrameRate(videoFramesPerSecond);
                
                mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
                
                            
                      mediaRecorder.prepare();
                      mediaRecorder.start();   
                      return true;
                            
       } catch (IllegalStateException e) {
          Log.e(TAG,e.getMessage());
          e.printStackTrace();
          return false;
       } catch (IOException e) {
          Log.e(TAG,e.getMessage());
          e.printStackTrace();
          return false;
       }
    }
    
    public void stopRecording(){
       mediaRecorder.stop();
       camera.lock();
    }
}

So sad no one is willing to help me, despite i've tried my best to solve my own problem... Or is this section of the forum very non-active? Sad :(

So sad no one is willing to help me, despite i've tried my best to solve my own problem... Or is this section of the forum very non-active? Sad :(

You not only one who needs help on this forum. Unfortunately it is only me who is helping out in mobile development, but I have limited time. Therefore I would not look on some random code where someone have no idea what is going one and expect someone else to sort it out.
As far I know this is your school project and you will be judged on it so it is in your interested to provide more in detail problem description and also search other possibilities.

ok i understand your position, sorry if i made insensitive remarks or along the line...
But the thing is i'm really bad in programming and worse still i'm a total newbie in android developing yet my lecturer chose me to do this project, well maybe that's life as a student :(

But this is what i tried out so far for my project could you help me out in this, i've have been cracking my head for the past few days without any progress..


I had my service run the video recording processes in the background so i should be able to leave the app for a while and come back to the Video recording GUI with the surfaceview but my application was force to close when i return to the app. So is the problem related to onResume() method or something else like just geting my GUI preview the camera instance or what? If its yes what i should do in order to get my application GUI to return to the video recording surfaceview and still running the background recording using the service rather than giving me an error?

public class CameraTest extends Activity implements SurfaceHolder.Callback {
   
   private static final String TAG = "Exception";
   
   public static SurfaceView surfaceView;
   public static SurfaceHolder surfaceHolder;
   public static Camera camera;
   public static boolean previewRunning;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        surfaceView = (SurfaceView)findViewById(R.id.surface_camera);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        
        Button btnStart = (Button) findViewById(R.id.button4);
        btnStart.setOnClickListener(new View.OnClickListener() 
        {
           public void onClick(View v)
           {
              startService(new Intent(getApplicationContext(), ServiceRecording.class));
           }
        });
        
        Button btnStop = (Button) findViewById(R.id.button5);
        btnStop.setOnClickListener(new View.OnClickListener() 
        {
           public void onClick(View v)
           {
              stopService(new Intent(getApplicationContext(), ServiceRecording.class));
           }
        });
    }
    
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
       camera = Camera.open();
       if (camera != null) {
          Camera.Parameters params = camera.getParameters();
          camera.setParameters(params);
       }
       else {
          Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
          finish();
       }
    }
    
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
       if (previewRunning) {
          camera.stopPreview();
       }
       
       Camera.Parameters p = camera.getParameters();
       p.setPreviewSize(320, 240);
       p.setPreviewFormat(PixelFormat.JPEG);
       camera.setParameters(p);
       
       try {
             camera.setPreviewDisplay(holder);
             camera.startPreview();
             previewRunning = true;
       }
       catch (IOException e) {
          Log.e(TAG,e.getMessage());
          e.printStackTrace();
       }
    }
    
    @Override
    public void onResume(){
       super.onResume();
       
    }
    
    @Override
    public void surfaceDestroyed(SurfaceHolder holder){
          camera.stopPreview();
          previewRunning = false;
          camera.release();
    }
}

My RecordingService.java Service file

public class ServiceRecording extends Service {

   @Override
   public IBinder onBind(Intent intent) {
      // TODO Auto-generated method stub
      return null;
   }
   
   private SurfaceView surfaceView;
   private SurfaceHolder surfaceHolder;
   private Camera camera;
   private boolean recordingStatus;
   private MediaRecorder mediaRecorder;
    private final int maxDurationInMs = 20000;
    
    private static final String TAG = "Exception";
    
    @Override
    public void onCreate() {
       super.onCreate();
       
       recordingStatus = false;
       camera = CameraTest.camera;
       surfaceView = CameraTest.surfaceView;
       surfaceHolder = CameraTest.surfaceHolder;
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
       super.onStartCommand(intent, flags, startId);
       if (recordingStatus == false)
          startRecording();
    
       return START_STICKY;
    }
    
    @Override
    public void onDestroy() {
       super.onDestroy();
    
       stopRecording();
       //camera.stopPreview();
       recordingStatus = false;
       //camera.release();
    }   
    
    public boolean startRecording(){
       try {
             Toast.makeText(getBaseContext(), "Recording Started", Toast.LENGTH_SHORT).show();
             try{
                camera.unlock();
             }
             catch(Exception e){
                
                camera.reconnect();   
             }
             
             mediaRecorder = new MediaRecorder();
             
             mediaRecorder.setCamera(camera);
             
             mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    
             mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    
             mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
                
             mediaRecorder.setMaxDuration(maxDurationInMs);
                             
                mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
                
                mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
                
                //mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH_mm_ss");
                Date date = new Date();
                File directory = new File(Environment.getExternalStorageDirectory() + "/VideoList");
                
                if(!(directory.exists()))
                   directory.mkdir();
                   
                File FileSaved = new File(Environment.getExternalStorageDirectory() + "/VideoList", dateFormat.format(date) + ".3gp");
                mediaRecorder.setOutputFile(FileSaved.getPath());
                
                mediaRecorder.setVideoSize(surfaceView.getWidth(),surfaceView.getHeight());
                
                //mediaRecorder.setVideoFrameRate(videoFramesPerSecond);
                
                mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
                         
                mediaRecorder.prepare();
                mediaRecorder.start();   
                
                recordingStatus = true;
                
                return true;                  
       } 
       
       catch (IllegalStateException e) {
          Log.d(TAG,e.getMessage());
          e.printStackTrace();
          return false;
       } 
       catch (IOException e) {
          Log.d(TAG,e.getMessage());
          e.printStackTrace();
          return false;
       }
    }
    
    public void stopRecording() {
       Toast.makeText(getBaseContext(), "Recording Stopped", Toast.LENGTH_SHORT).show();   
       mediaRecorder.stop();
       try {
         camera.reconnect();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
       recordingStatus = false;
    }
}

Q1. What are the available different forms of video encoding type available in android for user to choose from? Because i'm currently doing a video recording app which i wanted to use ListPreference to allow user to dynamically choose what type of encoding is available.

Q2. I wanted to know is there any easy way to retrieve video resolution on the actual phone? Because different phone has different phone resolution's compatibility, so i wanted to retrieve the resolution data and display the different resolutions dynamically into my ListPreference for user selection... Does it have to do with a method name "flatten()" i don't quite know what is the method... I'm kinna new in java/android, can someone guide me on how should i do that?

My mainActivity.java file

public class CameraTest extends Activity implements SurfaceHolder.Callback {
	
	private static final String TAG = "Exception";
	
	public static SurfaceView surfaceView;
	public static SurfaceHolder surfaceHolder;
	public static Camera MainCamera;
	private static boolean previewRunning;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        surfaceView = (SurfaceView)findViewById(R.id.surface_camera);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        //getparameters
       
        Button btnStart = (Button) findViewById(R.id.button4);
        btnStart.setOnClickListener(new View.OnClickListener() 
        {
        	public void onClick(View v)
        	{
        		startService(new Intent(getApplicationContext(), ServiceRecording.class));
        	}
        });
        
        Button btnStop = (Button) findViewById(R.id.button5);
        btnStop.setOnClickListener(new View.OnClickListener() 
        {
        	public void onClick(View v)
        	{
        		stopService(new Intent(getApplicationContext(), ServiceRecording.class));
        	}
        });
        
        Button btnSetting = (Button) findViewById(R.id.button2);
        btnSetting.setOnClickListener(new View.OnClickListener() 
        {
        	public void onClick(View v)
        	{
        		startActivity(new Intent(getApplicationContext(), SettingActivity.class));
        	}
        });
    }
    
    public static boolean ServiceStatus;
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    	
    	if(ServiceRecording.recordingStatus)
    	{
    		stopService(new Intent(getApplicationContext(), ServiceRecording.class));
    		
    		try {
				Thread.sleep(4000);
			} 
    		catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			MainCamera = ServiceRecording.ServiceCamera;
    		
			startService(new Intent(getApplicationContext(), ServiceRecording.class));	    		
    	}
    	else{
    		
    		MainCamera = Camera.open();
    	
    		if (MainCamera != null) {
        		Camera.Parameters params = MainCamera.getParameters();
        		params.setPreviewSize(320, 240);
            	params.setPreviewFormat(PixelFormat.JPEG);
            	MainCamera.setParameters(params);
            	
            	try {
					MainCamera.setPreviewDisplay(holder);
				} 
            	
            	catch (IOException e) 
				{
					e.printStackTrace();
				}
        		MainCamera.startPreview();
        		previewRunning = true;	
        	}
        	else {
        		Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
        		finish();
        	}	
    	}
    	
    	if (previewRunning) {
    		MainCamera.stopPreview();
    	}
    	
          /*ServiceStatus = ServiceRecording.recordingStatus;
          if(ServiceStatus == true)
          {
       	   stopService(new Intent(getApplicationContext(), ServiceRecording.class));
       	   
       	   	if (MainCamera != null) {
       		   Camera.Parameters params = MainCamera.getParameters();
       		   MainCamera.setParameters(params);
       		   MainCamera.startPreview();
       	   	}
          }*/	   
    }
    
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    	
    }
    
    @Override
    public void surfaceDestroyed(SurfaceHolder holder){
    	MainCamera.stopPreview();
    	previewRunning = false;
    	MainCamera.release();
    }  
}

My Preference.xml file

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
	<PreferenceCategory
                android:title="Video">
                <CheckBoxPreference
                        android:title="Audio"
                        android:defaultValue="True"
                        android:summary="Select w/o Audio"
                        android:key="AudioPref" />
                <ListPreference
                        android:title="Video Encoder"
                        android:defaultValue="3"
                        android:summary="Set the Video Encoding Type"
                        android:key="EncodingPref"
                        android:entries="@array/EncodingType"
                        android:entryValues="@array/updateEncodingType"
                        android:dialogTitle="Video Encoding Format" />
                <ListPreference
                        android:title="File Format"
                        android:defaultValue="3gp"
                        android:summary="Select the File Format"
                        android:key="FileFormatPref"
                        android:entries="@array/FileFormat"
                        android:entryValues="@array/updateFileFormat"
                        android:dialogTitle="File Format" />
                <ListPreference
                        android:title="Video Resolution"
                        android:defaultValue=""
                        android:summary="Set the video resolution"
                        android:key="ResolutionPref"
                        android:dialogTitle="Video Resolution" />
        </PreferenceCategory>        
</PreferenceScreen>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.