Android摄像头:静态图片

时间:2012-03-27 02:47:56

标签: android video camera

我使用相机意图来捕捉视频。这是问题所在: 如果我使用这行代码,我可以录制视频。但是onActivityResult不起作用。

    Intent intent = new Intent("android.media.action.VIDEO_CAMERA");

如果我使用这行代码,按下录制按钮后,相机会冻结,我的意思是, 图片还在。

    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

顺便说一句,当我使用Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);来捕捉图片时,它可以正常工作。

java文件如下:

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

1 个答案:

答案 0 :(得分:0)

使用MediaStore.ACTION_VIDEO_CAPTURE录制视频。

Intent photoPickerIntent= new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
              startActivityForResult(Intent.createChooser(photoPickerIntent,"Take Video"),TAKE_VIDEO);

您的OnActivityResult必须像这样才能将视频保存到特定位置:

if(requestCode==TAKE_VIDEO)
            {


                try
                {
                    Log.e("videopath","videopath");
                AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(data.getData(), "r");
                FileInputStream fis = videoAsset.createInputStream();
                File root=new File(Environment.getExternalStorageDirectory(),"Directory");

                  if (!root.exists()) {
                      root.mkdirs();
                  }
                  File file;

                   file = new File(root,filename+".mp4" );

                 Uri uri=Uri.fromFile(file);
                 Item1.videopath=uri.getPath();

                FileOutputStream fos = new FileOutputStream(file);

                byte[] buf = new byte[1024];
                int len;
                while ((len = fis.read(buf)) > 0) {
                    fos.write(buf, 0, len);
                }       
                fis.close();
                fos.close();


              } 
            catch (Exception e) 
            {
               e.printStackTrace();
            }
            }