Android拍照、录像、录音代码范例

  1. <p>import java.io.File;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.Date;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.database.Cursor;  
  7. import android.net.Uri;  
  8. import android.os.Bundle;  
  9. import android.os.Environment;  
  10. import android.provider.MediaStore;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.Toast;</p><p>public class ActivityMedia extends Activity  implements OnClickListener {    
  15.   private static final int RESULT_CAPTURE_IMAGE = 1;// 照相的requestCode    
  16.  private static final int REQUEST_CODE_TAKE_VIDEO = 2;// 摄像的照相的requestCode    
  17.  private static final int RESULT_CAPTURE_RECORDER_SOUND = 3;// 录音的requestCode    
  18.       
  19.          private String strImgPath = "";// 照片文件绝对路径    
  20.          private String strVideoPath = "";// 视频文件的绝对路径    
  21.          private String strRecorderPath = "";// 录音文件的绝对路径    
  22.            
  23.          Button buttonShot;  
  24.          Button buttonVideo;  
  25.          Button buttonRecorder;  
  26.      
  27.          @Override    
  28.          protected void onCreate(Bundle savedInstanceState) {    
  29.                  super.onCreate(savedInstanceState);    
  30.                  this.setContentView(R.layout.media);    
  31.                  buttonShot = (Button)findViewById(R.id.ButtonShot);  
  32.                  buttonShot.setOnClickListener(this);  
  33.                  buttonVideo = (Button)findViewById(R.id.ButtonVideo);  
  34.                  buttonVideo.setOnClickListener(this);  
  35.                  buttonRecorder = (Button)findViewById(R.id.ButtonRecorder);  
  36.                  buttonRecorder.setOnClickListener(this);  
  37.          }    
  38.      
  39.          @Override    
  40.          protected void onActivityResult(int requestCode, int resultCode, Intent data) {    
  41.                  super.onActivityResult(requestCode, resultCode, data);    
  42.                  switch (requestCode) {    
  43.                 case RESULT_CAPTURE_IMAGE://拍照    
  44.                          if (resultCode == RESULT_OK) {    
  45.                                  Toast.makeText(this, strImgPath, Toast.LENGTH_SHORT).show();    
  46.                          }    
  47.                          break;    
  48.                  case REQUEST_CODE_TAKE_VIDEO://拍摄视频    
  49.                          if (resultCode == RESULT_OK) {    
  50.                                  Uri uriVideo = data.getData();    
  51.                                  Cursor cursor=this.getContentResolver().query(uriVideo, null, null, null, null);    
  52.                                  if (cursor.moveToNext()) {    
  53.                                          /* _data:文件的绝对路径 ,_display_name:文件名 */    
  54.                                          strVideoPath = cursor.getString(cursor.getColumnIndex("_data"));    
  55.                                          Toast.makeText(this, strVideoPath, Toast.LENGTH_SHORT).show();    
  56.                                  }    
  57.                          }    
  58.                          break;    
  59.                  case RESULT_CAPTURE_RECORDER_SOUND://录音    
  60.                          if (resultCode == RESULT_OK) {    
  61.                                  Uri uriRecorder = data.getData();    
  62.                                 Cursor cursor=this.getContentResolver().query(uriRecorder, null, null, null, null);    
  63.                                 if (cursor.moveToNext()) {    
  64.                                          /* _data:文件的绝对路径 ,_display_name:文件名 */    
  65.                                          strRecorderPath = cursor.getString(cursor.getColumnIndex("_data"));    
  66.                                          Toast.makeText(this, strRecorderPath, Toast.LENGTH_SHORT).show();    
  67.                                  }    
  68.                          }    
  69.                          break;    
  70.                  }    
  71.          }    
  72.              
  73.              
  74.     
  75.          /**  
  76.           * 照相功能  
  77.           */    
  78.          private void cameraMethod() {    
  79.                  Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    
  80.                  strImgPath = Environment.getExternalStorageDirectory().toString() + "/CONSDCGMPIC/";//存放照片的文件夹    
  81.                  String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".jpg";//照片命名    
  82.                  File out = new File(strImgPath);    
  83.                  if (!out.exists()) {    
  84.                          out.mkdirs();    
  85.                  }    
  86.                  out = new File(strImgPath, fileName);    
  87.                  strImgPath = strImgPath + fileName;//该照片的绝对路径    
  88.                 Uri uri = Uri.fromFile(out);    
  89.                  imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);    
  90.                  imageCaptureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);    
  91.                  startActivityForResult(imageCaptureIntent, RESULT_CAPTURE_IMAGE);    
  92.      
  93.          }    
  94.      
  95.          /**  
  96.           * 拍摄视频  
  97.           */    
  98.          private void videoMethod() {    
  99.                  Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);    
  100.                  intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);    
  101.                  startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO);    
  102.         }    
  103.      
  104.         /**  
  105.           * 录音功能  
  106.           */    
  107.         private void soundRecorderMethod() {    
  108.                  Intent intent = new Intent(Intent.ACTION_GET_CONTENT);    
  109.                 intent.setType("audio/amr");    
  110.                  startActivityForResult(intent, RESULT_CAPTURE_RECORDER_SOUND);    
  111.          }    
  112.      
  113.          /**  
  114.           * 提示信息  
  115.           * @param text  
  116.           * @param duration  
  117.           */    
  118.         private void showToast(String text, int duration) {    
  119.                  Toast.makeText(ActivityMedia.this, text, duration).show();    
  120.         }</p><p>   public void onClick(View v) {  
  121.     int id = v.getId();  
  122.     switch(id){  
  123.     case R.id.ButtonShot:  
  124.      cameraMethod();  
  125.      break;  
  126.     case R.id.ButtonVideo:  
  127.      videoMethod();  
  128.      break;  
  129.     case R.id.ButtonRecorder:  
  130.      soundRecorderMethod();  
  131.      break;  
  132.     }  
  133.    }    
  134.            
  135. }</p>  


界面布局:

[java] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ScrollView  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.     <LinearLayout  
  7.       android:orientation="vertical"  
  8.       android:layout_width="fill_parent"  
  9.       android:layout_height="fill_parent">  
  10.       <Button  
  11.         android:id="@+id/ButtonShot"  
  12.         android:layout_width="fill_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="拍照"/>  
  15.       <Button  
  16.         android:id="@+id/ButtonVideo"  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:text="录像"/>  
  20.       <Button  
  21.         android:id="@+id/ButtonRecorder"  
  22.         android:layout_width="fill_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:text="录音"/>  
  25.     </LinearLayout>  
  26. </ScrollView>  


 

manifest添加权限:

[java] view plaincopy
 
    1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>  
    2.  <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>  
    3.  <uses-permission android:name="android.permission.GET_TASKS"></uses-permission>  
    4.  <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>  
    5.  <uses-permission android:name="android.permission.CAMERA"></uses-permission> 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。