正文  多媒体 > 录音 >

Android拍照、录像、录音代码

Android拍照、录像、录音代码[文件] RecordActivity.java001 package com.cons.dcg.collect;002 003 import java.io.File;004 import java.text.SimpleDateFormat;005 import java.util.*;006 import......

 Android拍照、录像、录音代码
[文件] RecordActivity.java
001 package com.cons.dcg.collect;
002
003 import java.io.File;
004 import java.text.SimpleDateFormat;
005 import java.util.*;
006 import android.app.*;
007 import android.content.Intent;
008 import android.database.Cursor;
009 import android.net.Uri;
010 import android.os.AsyncTask;
011 import android.os.Bundle;
012 import android.os.Environment;
013 import android.provider.MediaStore;
014 import android.view.*;
015 import android.widget.*;
016
017 public class RecordActivity extends Activity implementsOnClickListener {
018
019 private static final int RESULT_CAPTURE_IMAGE = 1;// 照相的requestCode
020 private static final int REQUEST_CODE_TAKE_VIDEO = 2;// 摄像的照相的requestCode
021 private static final int RESULT_CAPTURE_RECORDER_SOUND = 3;// 录音的requestCode
022
023 private String strImgPath = "";// 照片文件绝对路径
024 private String strVideoPath = "";// 视频文件的绝对路径
025 private String strRecorderPath = "";// 录音文件的绝对路径
026
027 @Override
028 protected void onCreate(Bundle savedInstanceState) {
029 super.onCreate(savedInstanceState);
030 this.setContentView(R.layout.problem_report);
031 }
032
033 @Override
034 protected void onActivityResult(int requestCode, intresultCode, Intent data) {
035 super.onActivityResult(requestCode, resultCode, data);
036 switch (requestCode) {
037 case RESULT_CAPTURE_IMAGE://拍照
038 if (resultCode == RESULT_OK) {
039 Toast.makeText(this, strImgPath, Toast.LENGTH_SHORT).show();
040 }
041 break;
042 case REQUEST_CODE_TAKE_VIDEO://拍摄视频
043 if (resultCode == RESULT_OK) {
044 Uri uriVideo = data.getData();
045 Cursor cursor=this.getContentResolver().query(uriVideo, null, null, null,null);
046 if (cursor.moveToNext()) {
047 /** _data:文件的绝对路径 ,_display_name:文件名 */
048 strVideoPath = cursor.getString(cursor.getColumnIndex("_data"));
049 Toast.makeText(this, strVideoPath, Toast.LENGTH_SHORT).show();
050 }
051 }
052 break;
053 case RESULT_CAPTURE_RECORDER_SOUND://录音
054 if (resultCode == RESULT_OK) {
055 Uri uriRecorder = data.getData();
056 Cursor cursor=this.getContentResolver().query(uriRecorder, null, null, null,null);
057 if (cursor.moveToNext()) {
058 /** _data:文件的绝对路径 ,_display_name:文件名 */
059 strRecorderPath = cursor.getString(cursor.getColumnIndex("_data"));
060 Toast.makeText(this, strRecorderPath, Toast.LENGTH_SHORT).show();
061 }
062 }
063 break;
064 }
065 }
066
067
068
069 /**
070 * 照相功能
071 */
072 private void cameraMethod() {
073 Intent imageCaptureIntent = newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
074 strImgPath = Environment.getExternalStorageDirectory().toString() +"/CONSDCGMPIC/";//存放照片的文件夹
075 String fileName = newSimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".jpg";//照片命名
076 File out = new File(strImgPath);
077 if (!out.exists()) {
078 out.mkdirs();
079 }
080 out = new File(strImgPath, fileName);
081 strImgPath = strImgPath + fileName;//该照片的绝对路径
082 Uri uri = Uri.fromFile(out);
083 imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
084 imageCaptureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);
085 startActivityForResult(imageCaptureIntent, RESULT_CAPTURE_IMAGE);
086
087 }
088
089 /**
090 * 拍摄视频
091 */
092 private void videoMethod() {
093 Intent intent = newIntent(MediaStore.ACTION_VIDEO_CAPTURE);
094 intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
095 startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO);
096 }
097
098 /**
099 * 录音功能
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 }