Android启动相机拍照并返回图片
作者:mrr 时间:2022-06-08 17:29:07
具体实现过程请看下面代码:
简单的调用了一下系统的拍照功能
代码如下所示:
//拍照的方法
private void openTakePhoto(){
/**
* 在启动拍照之前最好先判断一下sdcard是否可用
*/
String state = Environment.getExternalStorageState(); //拿到sdcard是否可用的状态码
if (state.equals(Environment.MEDIA_MOUNTED)){ //如果可用
Intent intent = newIntent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent,TAKE_PHOTO);
}else {
Toast.makeText(SettingActivity.this,"sdcard不可用",Toast.LENGTH_SHORT).show();
}
}
拍完照片后,我们肯定视需要拿到我们已经拍好的照片去进行一些操作(例如,换头像、发带图状态等等),这时候我们就需要重写onActivityResult()方法,去拿到我们拍好的照片去进行操作,我们可以通过下面两种方式去拿到我们的照片,他的加过就封装在了我们的Intent里面,我们只需要取出来即可:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intentdata) {
super.onActivityResult(requestCode, resultCode, data);
if (data!= null) {
switch (requestCode) {
case TAKE_PHOTO: //拍摄图片并选择
//两种方式 获取拍好的图片
if (data.getData() != null|| data.getExtras() != null){ //防止没有返回结果
Uri uri =data.getData();
if (uri != null) {
photo =BitmapFactory.decodeFile(uri.getPath()); //拿到图片
}
if (photo == null) {
Bundle bundle =data.getExtras();
if (bundle != null){
photo =(Bitmap) bundle.get("data");
} else {
Toast.makeText(getApplicationContext(), "找不到图片",Toast.LENGTH_SHORT).show();
}
}
//处理图片
//裁剪图片
}
break;
以上代码附有注释,相信大家都可以看得吧,有哪些不明白的地方欢迎各位朋友给留言,我会在第一时间和大家取得联系的。谢谢!
标签:android,相机拍照,返回图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在Android中使用WebSocket实现消息通信的方法详解
2022-06-10 06:26:18
![](https://img.aspxhome.com/file/2023/4/98664_0s.png)
Spring MVC 基于URL的映射规则(注解版)
2021-05-23 15:05:09
Android编辑框EditText与焦点变更监视器及文本变化监视器实现流程详解
2021-06-21 04:48:14
![](https://img.aspxhome.com/file/2023/6/98676_0s.png)
MyBatis-plus中的模糊查询解读
2022-06-16 08:27:03
![](https://img.aspxhome.com/file/2023/5/61665_0s.png)
被kafka-client和springkafka版本坑到自闭及解决
2023-08-23 15:07:36
![](https://img.aspxhome.com/file/2023/2/58452_0s.png)
GSON实现Java对象与JSON格式对象相互转换的完全教程
2023-11-23 09:23:37
C#中事件处理的个人体会
2023-11-02 02:14:39
Spring Boot 整合持久层之Spring Data JPA
2022-07-29 04:00:38
![](https://img.aspxhome.com/file/2023/9/62689_0s.png)
Java遗传算法之冲出迷宫
2022-01-12 21:34:58
Maven的几个常用plugin
2021-12-05 13:36:28
maven手动上传jar包示例及图文步骤过程
2023-11-13 22:10:57
![](https://img.aspxhome.com/file/2023/1/59571_0s.png)
如何通过XML方式配置并实现Mybatis
2021-12-21 02:49:04
![](https://img.aspxhome.com/file/2023/0/125340_0s.png)
MyBatis Log 插件无法显示SQL语句的原因解析
2023-11-24 23:42:55
Java获取视频时长、大小的示例
2023-01-16 01:48:44
android开发之listView组件用法实例简析
2023-02-05 10:31:52
![](https://img.aspxhome.com/file/2023/6/120166_0s.png)
Spring IOC基于注解启动示例详析
2022-04-19 22:49:55
![](https://img.aspxhome.com/file/2023/9/87219_0s.jpg)
maven工程中jar包瘦身的五种方法
2023-11-04 05:01:54
javaWeb项目部署到阿里云服务器步骤详解
2023-11-07 05:21:36
![](https://img.aspxhome.com/file/2023/9/58909_0s.png)
Java C++ 算法leetcode828统计子串中唯一字符乘法原理
2022-05-09 11:19:18
![](https://img.aspxhome.com/file/2023/8/61118_0s.png)
spring mvc 组合mybatis框架实例详解
2023-11-28 04:56:04
![](https://img.aspxhome.com/file/2023/2/59752_0s.png)