欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-03-04 10:51:23
本文主要介绍android实现拍照、录像、录音代码的资料,这里整理了详细的代码,有需要的小伙伴可以参考下。 recordactivity.java &nbs...

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

recordactivity.java   

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();
    }
}

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