Android Camera系列开发 (二)通过Intent录制视频

Android Camera系列开发 (二)通过Intent录制视频

作者:雨水  2013-8-18 CSDN博客:http://blog.csdn.net/gobitan/

概述

使用Camera有两种方式:通过Intent方式和通过Camera的API。在开发系列(一) 中已经介绍了通过Intent方式拍照,本文介绍通过Intent的方式录制视频。


通过Itent实现拍录制视频

第一步:在Eclipse中创建一个名为AndroidCamera的Android工程,可参见Helloworld的例子;

第二步:在AndroidManifest.xml中添加使用Camera相关的声明如下:

 <uses-feature android:name="android.hardware.camera" android:required="false" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />	
   

第三步:编写 AndroidCameraActivity类,如下: \

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Toast;

public class AndroidCameraActivity extends Activity {
    private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;  
      
    private Intent intent  = null;  
    private Uri fileUri    = null;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
          
        intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);//create a intent to record video  
        fileUri = getOutputMediaFileUri(); // create a file Uri to save the video
        
        // set the video file name
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);   
        
        // set the video quality high
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 

        // start the video capture Intent
        startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);  
    }  
  
    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        super.onActivityResult(requestCode, resultCode, data);  
          
        if(requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {  
            if (resultCode == RESULT_OK) {  
                // video captured and saved to fileUri specified in the Intent  
                Toast.makeText(this, "Video saved to:\n" +  
                         data.getData(),   
                         Toast.LENGTH_LONG).show();  
            } else if (resultCode == RESULT_CANCELED) {  
                // User cancelled the video capture  
            }  
        }  
    }  
      
    /** Create a File Uri for saving a video */  
    private static Uri getOutputMediaFileUri(){  
    	//get the mobile Pictures directory
    	File picDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

        //get the current time
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
        
        File videoFile = new File(picDir.getPath() + File.separator + "VIDEO_"+ timeStamp + ".mp4");  
  
        return Uri.fromFile(videoFile);
    }  
}


第四步:运行程序。


运行程序会出现录制视频的窗口,录制保存后可以即可在SD卡的Pictures目录下找到刚才录制的视频。

参考资料

  1. http://developer.android.com/guide/topics/media/camera.html

 

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gobitan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值