您的位置:首页 > 移动开发 > Android开发

Android调用手机摄像头拍照和录音功能

2016-09-28 19:07 561 查看
调用摄像头拍照:

public class MainActivity extends Activity {

private Button button;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView= (ImageView) findViewById(R.id.imageView);
button= (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,1);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
Bundle bundle=data.getExtras();
Bitmap bitmap= (Bitmap) bundle.get("data");
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file=new File(Environment.getExternalStorageDirectory(),"MyImage");
if(!file.exists()){
file.mkdir();
}
try {
String date=new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
String path=file+"/"+date+".jpg";
FileOutputStream outputStream=new FileOutputStream(path);
bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
imageView.setImageBitmap(bitmap);
}
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp" />
</LinearLayout>


调用录音功能:
public class Main2Activity extends Activity implements View.OnClickListener,AdapterView.OnItemClickListener{

private ListView listView;//录音文件控件
private Button btn1,btn2;//开始按钮和停止按钮
private MediaRecorder recorder;//录音对象
private List<String> list=new ArrayList<>();//录音文件数据源
private File path,recorderFile;//根目录,要存入sd卡的录音文件
private ArrayAdapter adapter;//适配器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
init();
if(null!=path){
musicList();
}
}

//初始化时获得所有录音文件
private void musicList() {
File home=path;
//判断文件过滤器的长度是否大于0,大于则适配到listview上,小于则不设置上去
if(home.listFiles(new MusicFilter()).length>0){
for(File file:home.listFiles(new MusicFilter())){
list.add(file.getName());
}
adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
}
}

private void init() {
listView= (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(this);
btn1= (Button) findViewById(R.id.start);
btn2= (Button) findViewById(R.id.stop);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
path=getPath();//获得根目录
}

private File getPath() {
File file=null;
//判断sd卡状态
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
file=Environment.getExternalStorageDirectory();
}else{
Toast.makeText(this,"没有SD卡",Toast.LENGTH_SHORT).show();
}
return file;
}

@Override
public void onClick(View view) {
switch (view.getId()){
//开始按钮
case R.id.start:
startRecorder();
btn1.setEnabled(false);
btn2.setEnabled(true);
break;
//停止按钮
case R.id.stop:
stopRecorder();
btn1.setEnabled(true);
btn2.setEnabled(false);
break;
}
}

private void stopRecorder() {
//如果录音的文件不为null
if(recorderFile!=null){
//停止录音
recorder.stop();
//把录音文件的名字加入集合里
list.add(recorderFile.getName());
if(adapter!=null){
//刷新适配器
adapter.notifyDataSetChanged();
}
//释放录音对象
recorder.release();
recorder=null;
}

}

private void startRecorder() {
//创建录音对象
recorder=new MediaRecorder();
//设置麦克风
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//设置转码类型
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//设置编码方式
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
//创建录音文件
recorderFile=File.createTempFile("录音_",".amr",path);
//设置录音的数据写到录音文件里
recorder.setOutputFile(recorderFile.getAbsolutePath());
//录音准备
recorder.prepare();
//录音开始
recorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//获得点击条目的路径
File file=new File(path.getAbsolutePath()+File.separator+list.get(i));
playMusic(file);
}

//调用播放器播放点击的条目文件
private void playMusic(File file) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "audio/mp3");
startActivity(intent);
}
}
文件过滤代码:
public class MusicFilter implements FilenameFilter {
@Override
public boolean accept(File file, String name) {
return (name.endsWith(".amr"));
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/start"
android:text="开始录音"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/stop"
android:text="停止录音"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 摄像头 录音