第一弹
萌猫导航

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

作者: 小胖手 来源: 怪哈哈网 日期: 2016-10-24 15:21:20 人气: -


本文主要介绍Android实现拍照、录像、录音代码的资料,这里整理了详细的代码,有需要的小伙伴可以参考下。

RecordActivity.java    

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持怪哈哈。


转载请注明文章来源(欢迎分享): http://www.guaihaha.com/android/12138.html

休闲屋
关于怪哈哈
怪哈哈是一个集搞笑,美女,动漫,游戏,技术等为核心的综合性网站!
联系我们
商务洽谈、广告合作、友情链接、侵权举报,这些都可以联系我们哦!
关于我们 - 联系我们 - 广告服务 - 免责申明   
本站文字和图片均为严格审查筛选收藏,均收集整理于互联网,其著作权归原作者所有,如果有侵犯您权利的照片,请来信告知,我们将及时撤销相应文字和图片.
Copyright @2014-2020 怪哈哈网 All Rights Reserved.
【谢谢大家一直支持贵站】