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

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