【问题标题】:Android Video Camera : still pictureAndroid 摄像机:静态图片
【发布时间】:2012-03-27 23:45:34
【问题描述】:

我使用相机意图来捕捉视频。这是问题所在: 如果我使用这行代码,我可以录制视频。但是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());
    }
}

【问题讨论】:

    标签: android video camera


    【解决方案1】:

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

    【讨论】:

    • 谢谢你。我是 Android 的新手。该应用程序旨在录制视频并通过网络传输到 PC。你能给我任何指南或一些我可以参考的文章吗? @Abhi
    • 这两行代码是干什么用的? Uri uri=Uri.fromFile(file);Item1.videopath=uri.getPath();@Abhi
    • 获取视频在SD卡中的存储路径... trasmit表示要上传到服务器?
    • 我想边录边把视频传到电脑上,也就是实时录视频再传。同时,PC 或服务器可以播放视频。它喜欢把安卓手机当成监控摄像头,而把电脑当成屏幕。@Abhi
    猜你喜欢
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多