Android Video Camera : still picture
- by Alex
I use the camera intent to capture video. Here is the problem:
If I use this line of code, I can record video. But onActivityResult doesn't work.
Intent intent = new Intent("android.media.action.VIDEO_CAMERA");
If I use this line of code, after press the recording button, the camera is freezed, I mean,
the picture is still.
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
BTW, when I use $Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); to capture a picture, it works fine.
The java file is as follows:
package com.camera.picture;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.VideoView;
public class PictureCameraActivity extends Activity {
private static final int IMAGE_CAPTURE = 0;
private static final int VIDEO_CAPTURE = 1;
private Button startBtn;
private Button videoBtn;
private Uri imageUri;
private Uri videoUri;
private ImageView imageView;
private VideoView videoView;
/** Called when the activity is first created.
* sets the content and gets the references to
* the basic widgets on the screen like
* {@code Button} or {@link ImageView}
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView)findViewById(R.id.img);
videoView = (VideoView)findViewById(R.id.videoView);
startBtn = (Button) findViewById(R.id.startBtn);
startBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startCamera();
}
});
videoBtn = (Button) findViewById(R.id.videoBtn);
videoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startVideoCamera();
}
});
}
public void startCamera() {
Log.d("ANDRO_CAMERA", "Starting camera on the phone...");
String fileName = "testphoto.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,
"Image capture by camera");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, IMAGE_CAPTURE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IMAGE_CAPTURE) {
if (resultCode == RESULT_OK){
Log.d("ANDROID_CAMERA","Picture taken!!!");
imageView.setImageURI(imageUri);
}
}
if (requestCode == VIDEO_CAPTURE) {
if (resultCode == RESULT_OK) {
Log.d("ANDROID_CAMERA","Video taken!!!");
Toast.makeText(this, "Video saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
videoView.setVideoURI(videoUri);
}
}
}
private void startVideoCamera() {
// TODO Auto-generated method stub
//create new Intent
Log.d("ANDRO_CAMERA", "Starting camera on the phone...");
String fileName = "testvideo.mp4";
ContentValues values = new ContentValues();
values.put(MediaStore.Video.Media.TITLE, fileName);
values.put(MediaStore.Video.Media.DESCRIPTION,
"Video captured by camera");
values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
videoUri = getContentResolver().insert(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent("android.media.action.VIDEO_CAMERA");
//Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
// start the Video Capture Intent
startActivityForResult(intent, VIDEO_CAPTURE);
}
private static File getOutputMediaFile() {
// TODO Auto-generated method stub
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
return mediaFile;
}
/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(){
return Uri.fromFile(getOutputMediaFile());
}
}