1. Download our Official Android App: Forums for Android!

Apps How do i name my video file in the format of DateTime?

Discussion in 'Android Development' started by imso, Apr 2, 2011.

  1. imso

    imso Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    56
    Joined:
    Mar 8, 2011

    Mar 8, 2011
    56
    0
    15
    I want my video file format such that every newly recorded video file will be name its current date and time when it is being recorded and saved into an folder in an sd card? Eg: 2011-04-01 16:49:15 Can someone help me please, i tried reading lots of site on this but still not knowing how to do it?? Is it using import java.util or something else can someone help me do i need some sort of method in order to do this?? I'm very new to eclipse and java can someone guide me on this...

    Code (Text):
    1. public class CameraTest extends Activity implements SurfaceHolder.Callback {
    2.    
    3.    private static final String TAG ="CAMERA_TUTORIAL";
    4.    
    5.    private SurfaceView surfaceView;
    6.    private SurfaceHolder surfaceHolder;
    7.    private Camera camera;
    8.    private boolean previewRunning;
    9.  
    10.    File tempFile = null;
    11.    
    12.     @Override
    13.     public void onCreate(Bundle savedInstanceState) {
    14.         super.onCreate(savedInstanceState);
    15.         setContentView(R.layout.main);
    16.        
    17.         surfaceView = (SurfaceView)findViewById(R.id.surface_camera);
    18.         surfaceHolder = surfaceView.getHolder();
    19.         surfaceHolder.addCallback(this);
    20.        
    21.         surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    22.        
    23.         Button btnStart = (Button) findViewById(R.id.button4);
    24.         btnStart.setOnClickListener(new View.OnClickListener()
    25.         {
    26.            public void onClick(View v)
    27.            {
    28.               startRecording();
    29.               Toast.makeText(getBaseContext(), "Recording Started", Toast.LENGTH_SHORT).show();
    30.            }
    31.            
    32.         });
    33.        
    34.         Button btnStop = (Button) findViewById(R.id.button5);
    35.         btnStop.setOnClickListener(new View.OnClickListener()
    36.         {
    37.            public void onClick(View v)
    38.            {
    39.               stopRecording();
    40.               Toast.makeText(getBaseContext(), "Recording Stopped", Toast.LENGTH_SHORT).show();  
    41.            }
    42.         });
    43.     }
    44.     @Override
    45.     public void surfaceCreated(SurfaceHolder holder) {
    46.        camera = Camera.open();
    47.        if (camera != null) {
    48.           Camera.Parameters params = camera.getParameters();
    49.           camera.setParameters(params);
    50.        }
    51.        else {
    52.           Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
    53.           finish();
    54.        }
    55.     }
    56.    
    57.     @Override
    58.     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    59.        if (previewRunning) {
    60.           camera.stopPreview();
    61.        }
    62.        Camera.Parameters p = camera.getParameters();
    63.        p.setPreviewSize(320, 240);
    64.        p.setPreviewFormat(PixelFormat.JPEG);
    65.        camera.setParameters(p);
    66.        
    67.        try {
    68.              camera.setPreviewDisplay(holder);
    69.              camera.startPreview();
    70.              previewRunning = true;
    71.        }
    72.        catch (IOException e) {
    73.           Log.e(TAG,e.getMessage());
    74.           e.printStackTrace();
    75.        }
    76.     }
    77.    
    78.     @Override
    79.     public void surfaceDestroyed(SurfaceHolder holder){
    80.           camera.stopPreview();
    81.           previewRunning = false;
    82.           camera.release();
    83.     }
    84.    
    85.     private MediaRecorder mediaRecorder;
    86.     private final int maxDurationInMs = 20000;
    87.     private final int videoFramesPerSecond = 20;
    88.    
    89.     public boolean startRecording(){
    90.        try {
    91.              camera.unlock();
    92.              
    93.              mediaRecorder = new MediaRecorder();
    94.              
    95.              mediaRecorder.setCamera(camera);
    96.              
    97.              mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    98.    
    99.              mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    100.    
    101.              mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
    102.                
    103.              mediaRecorder.setMaxDuration(maxDurationInMs);
    104.                
    105.                
    106.                 mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    107.                
    108.                 mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
    109.                
    110.                 //mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
    111.                
    112.                 tempFile = new File(Environment.getExternalStorageDirectory(),"1.3gp");
    113.                 mediaRecorder.setOutputFile(tempFile.getPath());
    114.                
    115.                 mediaRecorder.setVideoSize(surfaceView.getWidth(),surfaceView.getHeight());
    116.                
    117.                 //mediaRecorder.setVideoFrameRate(videoFramesPerSecond);
    118.                
    119.                 mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
    120.                          
    121.                       mediaRecorder.prepare();
    122.                       mediaRecorder.start();  
    123.                       return true;
    124.                            
    125.        } catch (IllegalStateException e) {
    126.           Log.e(TAG,e.getMessage());
    127.           e.printStackTrace();
    128.           return false;
    129.        } catch (IOException e) {
    130.           Log.e(TAG,e.getMessage());
    131.           e.printStackTrace();
    132.           return false;
    133.        }
    134.     }
    135.    
    136.     public void stopRecording(){
    137.        mediaRecorder.stop();
    138.        camera.lock();
    139.     }
    140. }
     

    Advertisement

  2. pietjuhhh1990

    pietjuhhh1990 Well-Known Member
    Rank:
    None
    Points:
    38
    Posts:
    235
    Joined:
    Feb 21, 2011

    Feb 21, 2011
    235
    22
    38
    App/web development
    Netherlands
    System.currentTimeMillis()
     
  3. miXer

    miXer Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    313
    Joined:
    Aug 12, 2010

    Aug 12, 2010
    313
    51
    53
    Norway
    This will only give you the milliseconds. Thats not what he is after.
    This will print generate that date sentence you want:
    Code (Text):
    1. Calendar c = Calendar.getInstance();
    2.        
    3.         String date = c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH) + "-" + c.get(Calendar.DAY_OF_MONTH) + " " + c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND);
     
    GIR likes this.

Share This Page

Loading...