Android调用手机摄像头拍照和录音功能
作者:Daisuki_ch 时间:2022-10-22 15:37:16
本文实例为大家分享了Android调用手机摄像头拍照和录音功能的具体代码,供大家参考,具体内容如下
调用摄像头拍照:
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>
来源:https://blog.csdn.net/Daisuki_ch/article/details/52692957
标签:Android,摄像头,录音
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot持久化层操作支持技巧
2023-11-24 06:40:32
![](https://img.aspxhome.com/file/2023/3/59933_0s.png)
C#中Linq延迟查询的例子
2022-01-15 02:43:11
Java @Autowired报错原因分析和4种解决方案
2022-10-30 04:22:03
![](https://img.aspxhome.com/file/2023/6/79286_0s.webp)
从内存地址解析Java的static关键字的作用
2022-03-18 03:49:26
![](https://img.aspxhome.com/file/2023/2/86832_0s.png)
Android Flutter使用本地数据库编写备忘录应用
2023-09-15 17:24:09
![](https://img.aspxhome.com/file/2023/6/109226_0s.webp)
Java中单例模式的七种写法示例
2021-07-16 07:57:22
![](https://img.aspxhome.com/file/2023/9/75799_0s.png)
Java利用移位运算将int型分解成四个byte型的方法
2023-11-09 08:25:00
![](https://img.aspxhome.com/file/2023/4/59334_0s.png)
Java新手环境搭建 JDK8安装配置教程
2023-11-25 17:23:10
![](https://img.aspxhome.com/file/2023/1/60311_0s.jpg)
JavaSE实现猜拳游戏
2023-12-22 07:21:19
Java实现用户管理系统
2023-08-26 17:18:21
分析ABA问题的本质及其解决办法
2021-12-20 13:59:26
JVM Client和Server端有什么区别
2023-08-05 22:49:53
![](https://img.aspxhome.com/file/2023/5/64385_0s.png)
C#中数组初始化与数组元素复制的方法
2023-05-14 15:45:06
Java代码实现简单酒店管理系统
2023-11-21 03:38:57
![](https://img.aspxhome.com/file/2023/9/59539_0s.jpg)
C#计算字符串相似性的方法
2022-08-18 11:32:15
Android 8.0实现蓝牙遥控器自动配对
2021-08-05 08:24:16
C#对WPF数据绑定的菜单插入Seperator分隔
2022-04-29 17:09:28
MyBatis-Plus实现公共字段自动填充功能详解
2021-10-19 22:14:39
![](https://img.aspxhome.com/file/2023/2/84242_0s.png)
Mybatis-Plus的SQL语句组拼原理说明
2021-07-03 20:39:22
![](https://img.aspxhome.com/file/2023/5/84285_0s.png)
Spring Boot2中如何优雅地个性化定制Jackson实现示例
2021-09-27 12:21:15