zoukankan      html  css  js  c++  java
  • 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:
    " +  
                             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
  • 相关阅读:
    7月的尾巴,你是XXX
    戏说Android view 工作流程《下》
    “燕子”
    Android开机动画bootanimation.zip
    戏说Android view 工作流程《上》
    ViewController里已连接的IBOutlet为什么会是nil
    My first App "Encrypt Wheel" is Ready to Download!
    iOS开发中角色Role所产生的悲剧(未完)
    UIScrollView实现不全屏分页的小技巧
    Apple misunderstood my app,now my app status changed to “In Review”
  • 原文地址:https://www.cnblogs.com/riskyer/p/3266565.html
Copyright © 2011-2022 走看看