PDA

View Full Version : How do i name my video file in the format of DateTime?



imso
04-02-2011, 04:01 AM
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...


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_P USH_BUFFERS);

Button btnStart = (Button) findViewById(R.id.button4);
btnStart.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
startRecording();
Toast.makeText(getBaseContext(), "Recording Started", Toast.LENGTH_SHORT).show();
}

});

Button btnStop = (Button) findViewById(R.id.button5);
btnStop.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
stopRecording();
Toast.makeText(getBaseContext(), "Recording Stopped", Toast.LENGTH_SHORT).show();
}
});
}
@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.AudioSo urce.MIC);

mediaRecorder.setVideoSource(MediaRecorder.VideoSo urce.CAMERA);

mediaRecorder.setOutputFormat(MediaRecorder.Output Format.DEFAULT);

mediaRecorder.setMaxDuration(maxDurationInMs);


mediaRecorder.setAudioEncoder(MediaRecorder.AudioE ncoder.DEFAULT);

mediaRecorder.setVideoEncoder(MediaRecorder.VideoE ncoder.DEFAULT);

//mediaRecorder.setOutputFormat(MediaRecorder.Output Format.DEFAULT);

tempFile = new File(Environment.getExternalStorageDirectory(),"1.3gp");
mediaRecorder.setOutputFile(tempFile.getPath());

mediaRecorder.setVideoSize(surfaceView.getWidth(), surfaceView.getHeight());

//mediaRecorder.setVideoFrameRate(videoFramesPerSeco nd);

mediaRecorder.setPreviewDisplay(surfaceHolder.getS urface());

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();
}
}

AllTime
04-11-2011, 08:07 AM
Hi imso,

I don't know the confirm method to save your file to /sdcard as I am also a newbie in Android. But can definetely give you guidane in filename setup.

You will have to parse your Date & Time. Using java.text.SimpleDateFormat you can convert your date into appropriate format and get the string of it. For Eg: "yyyyMMddhhmmss" This will give you date as 20100411183010. Once you get this as a String, you can assign it as a fileName = dateStr + ".avi" => 20100411_183010.avi . This is the easiest and simplest way.

For using native style, you get date.toString() as "2010 4 11 18:30:10". You got to parse the string value by removing " ", ":", making sure the month, date, value is of 2 digit. If not add 0 ahead the value. With this you can get the value as you want and in whichever format. Like if you want _ between date & time, you can add here or so on. For this process, only String class will be used. Process will be longer to code but if DateFormat wont be helpful then this process will be the best for your file name creation.

Hope this helps.

imso
04-14-2011, 07:43 AM
I somehow code it this way but the system don't seems to allow to to display it in "2011-1-12 12:2:22", where ":" keeps giving me error... How can i solve this?


DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH_mm_ss");
Date date = new Date();
dateFormat.format(date)

Thanks...

AllTime
04-14-2011, 08:29 AM
("yyyy-MM-dd HH:mm:ss") should work. What error message are you getting ?
Also try making :
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Sometimes DateFormat df = new SimdateDateFormat(); doesn't work.

Try out. If still doesn't work show the error that you receive.

imso
04-14-2011, 08:14 PM
The reason i chose is that it seems to use "yyyy-MM-dd HH_mm_ss"); is it does not give me error give i do put it as "yyyy-MM-dd HH:mm:ss") , it gave me errors
as it seems like in windows if you try to use ":" to name your file it simply just don't allow you to do that...

So ss there a work around to allow me to display in the format of "2003-2-2 12:2:2" instead of "2003-2-2 12_2_2"


DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH_mm_ss");
Date date = new Date();
dateFormat.format(date);

AllTime
04-15-2011, 02:56 AM
Oh yes, for file name definetely can't use ":". Then what you have done is crrect and I guess you got your solution also.

Kindly mark my answer as "Helpful" to add to my reputation, if it has helped you gain your solution to some extend.

imso
04-15-2011, 03:35 AM
But i wanted it as ":" is there a way to make it display that?

AllTime
04-15-2011, 03:40 AM
But i wanted it as ":" is there a way to make it display that?

Its the Windows file naming convetion, it can't contain symbols among "/ \ : * ? < > |"
Something that Windows OS itself doesn't allow, then who can help ! Find out an alternative the at Windows will support.

prashu023rohilla
04-15-2011, 04:04 AM
Couldn't find anything similar when I searched so thought I'd throw this out there for use in file names in a Windows file system:
DateTime.Now.ToString ("yyyyMMddHHmmss")
Will output "201001312359"
Add a ".txt" or ".dat" if you're hard-core and you're all set.

imso
04-15-2011, 04:29 AM
So there won't be a easy work around??