android 拍照和从相册选择组件
android 拍照及从相册选择组件
单独封装到一个 activity 中便于更好的复用
拍照或从相册选择成功后使用 EventBus 发出广播回传图片路径,和调用者充分解耦合
根据传入参数支持裁剪和不裁剪两种模式
/**
* <pre>
* 拍照及从相册选择弹出 activity
* 成功后会发送 TakePhotoOutputEvent 事件,返回图片路径
* </pre>
*/
public class TakePhotoPopupActivity extends Activity {
public static final int REQUEST_CODE_CAMERA = 110;
public static final int REQUEST_CODE_ALBUM = 111;
public static final int REQUEST_CODE_CROP = 112; @ViewInject(R.id.takephoto_popup_layout)
private RelativeLayout outContainer;
@ViewInject(R.id.take_photo)
private TextView takePhoto;
@ViewInject(R.id.select_from_album)
private TextView openAlbum;
@ViewInject(R.id.cancel_photo)
private TextView cancel; private String photoFileSavePath;
private String croppedFileSavePath;
private boolean isCrop; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_takephoto_popup);
ViewUtils.inject(this); Intent intent = getIntent();
if (intent != null) {
photoFileSavePath = intent.getStringExtra("photoFileSavePath");
croppedFileSavePath = intent.getStringExtra("croppedFileSavePath");
isCrop = intent.getBooleanExtra("isCrop", false);
}
} @OnClick({ R.id.take_photo, R.id.select_from_album, R.id.cancel_photo, R.id.takephoto_popup_layout })
public void buttonOnclick(View v) {
switch (v.getId()) {
case R.id.take_photo:
openCamera(photoFileSavePath);
break;
case R.id.select_from_album:
openAlbum();
break;
case R.id.cancel_photo:
finish();
break;
case R.id.takephoto_popup_layout:
finish();
break;
}
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); try {
if (requestCode == REQUEST_CODE_CAMERA && resultCode == Activity.RESULT_OK) { // 相机
if (isCrop) {
// 裁剪
cropImage(photoFileSavePath, croppedFileSavePath);
} else {
// 不裁剪
EventBus.getDefault().post(new TakePhotoOutputEvent(requestCode, resultCode, photoFileSavePath));
finish();
}
} else if (requestCode == REQUEST_CODE_ALBUM && resultCode == Activity.RESULT_OK) { // 相册
try {
// 得到图片路径
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picPath = cursor.getString(columnIndex);
cursor.close(); if (isCrop) {
cropImage(picPath, croppedFileSavePath);
} else {
EventBus.getDefault().post(new TakePhotoOutputEvent(requestCode, resultCode, picPath));
finish();
}
} catch (Exception e) { }
} else if (requestCode == REQUEST_CODE_CROP && resultCode == Activity.RESULT_OK) { // 裁剪回来
EventBus.getDefault().post(new TakePhotoOutputEvent(requestCode, resultCode, croppedFileSavePath));
finish();
}
} catch (Exception e) {
e.printStackTrace();
}
} public void openAlbum() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent, REQUEST_CODE_ALBUM);
} /**
* 裁剪相册照片
*
*/
public void cropImage(String originFilePath, String croppedFilePath) {
File originFile = new File(originFilePath);
if (originFile == null || !originFile.exists()) {
return;
} try {
if (croppedFilePath == null) {
croppedFilePath = getExternalFilesDir(null) + "/" + "tmpcropped.png";
this.croppedFileSavePath = croppedFilePath;
} Uri originUri = Uri.fromFile(new File(originFilePath));
Uri croppedFileUri = Uri.fromFile(new File(croppedFilePath));
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(originUri, "image/*");
intent.putExtra("crop", true);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 120);
intent.putExtra("outputY", 120);
intent.putExtra("return-data", true);
intent.putExtra("output", croppedFileUri);
startActivityForResult(intent, REQUEST_CODE_CROP);
} catch (Exception e) {
e.printStackTrace();
}
} private void openCamera(String filePath) {
try {
if (filePath == null) {
filePath = getExternalFilesDir(null) + "/" + "tmpcamera.png";
this.photoFileSavePath = filePath;
} Uri uri = Uri.fromFile(new File(filePath));
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, REQUEST_CODE_CAMERA);
} catch (Exception e) {
e.printStackTrace();
}
}
}
demo github: https://github.com/lesliebeijing/AndroidPickPhotoComponent
android 拍照和从相册选择组件的更多相关文章
- Android 拍照或者从相册获取图片的实现
我们常常会用到上传头像,或者发帖子的时候选择本地图片上传的功能.这个很常见 今天因为app的需求我研究了下.现在分享下. 其实不论是通过拍照还是从相册选取都会用到Intent 这是系统提供给我们用来调 ...
- 浅谈Android中拍照、从相册选择图片并截图相关知识点
前言 我们在Android开发中经常会需要使用相机或者从相册中选取图片的情况,今天就把这里面相关的知识点总结下,方便以后开发的时候使用. 1.相机拍照并可自定义截图功能 我们先来看如何使用Intent ...
- android --拍照,从相册获取图片,兼容高版本,兼容小米手机
前几天做项目中选择图片的过程中遇到高版本和小米手机出现无法选择和崩溃的问题,现在记录下来,后面出现同类问题,也好查找 1,定义常量: private static final int TAKE_PIC ...
- vue+uniapp实现美颜拍照录像,相册选择 | 录像限制时长,美颜拍照录像
一.插件简介 Zhimi-BeautyCamera(智密 - 美颜相机图册插件)是一个支持美颜录像,美颜拍照,选择图视频功能,带录像参数时长限制的uniapp原生插件.平台支持:Android.IOS ...
- ng-cordova 手机拍照或从相册选择图片
1.需求描述 实现一个调用摄像头拍照,或者直接打开本地图库选择照片,然后替换App中图片的功能 2.准备 1) 安装ng-cordova 进入到ionic工程目录,使用bower工具安装, bower ...
- Android 拍照或从相册取图片并裁剪
在Android中,Intent触发Camera程序,拍好照片后,将会返回数据,但是考虑到内存问题,Camera不会将全尺寸的图像返回给调用的Activity,一般情况下,有可能返回的是缩略图,比如1 ...
- Android 拍照、从相册获取及裁剪的相关实现
首先这些功能都是通过Intent去启动系统的服务去实现的,所以自然就有相应的Action.相关Actiong如下: 拍照——MediaStore.ACTION_IMAGE_CAPTURE (" ...
- Android拍照和从相册获取照片
1.从相册获取照片 private void openAlumb() { //mRxPermissions:三方权限库 mRxPermissions .request(Manifest.permiss ...
- 在微信移动端input file拍照或从相册选择照片后会自动刷新页面退回到一开始网站进入的页面
<input type="file" accept="image/*"/> 调用打开摄像头后,聚焦后拍照,点击确认,这时页面会出现刷新动作,然后回退 ...
随机推荐
- 基于JDBC的数据库连接池技术研究与应用
引言 近年来,随着Internet/Intranet建网技术的飞速发展和在世界范围内的迅速普及,计算机 应用程序已从传统的桌面应用转到Web应用.基于B/S(Browser/Server)架构的3层开 ...
- mybatis 单表的增删改查
添加数据返回id mapper.xml mapper -> insert -> selectKey mybatis 内置别名
- Linq to SQL 中将数字转换为字符串
使用LINQ to Entities中的SqlFunctions调用数据库中的函数 添加引用System.Data.Entity 引用命名空间 using System.Data.Objects.Sq ...
- vs code开发.net core项目入门
今天用vs code来开发net core项目,写一下简要的开发流程,主要步骤如下,看完后你会发现特别简单 1.命令如下: (cmd中运行以下命令,下面命令都基于选择好自己的项目路径) 1.新建文件夹 ...
- linux网络NAT配置方式
NAT访问的权限如下: 外网不可以访问虚拟机,主机和虚拟机可以互访,网络和主机也可以互访: 1.打开虚拟机——编辑——虚拟网络编辑器——. 2. 3.进入虚拟机的linux系统点击网络 4. 5.点击 ...
- Ray tracing performance benchmark
accel. avg size 3.14accel. avg depth 16.15accel. max size 8accel. max depth 20accel. GPIT 3.00 MB tr ...
- [Perl][文件操作]判断文件是否为符号链接(Unicode路径)
Win32API::File 判断文件/文件夹是否为符号链接 Win32::Unicode 好像无法做这方面的判断,只能判断是否为目录.文件.文件是否存在. Win32API::File 则支持 Ge ...
- NOI2007 社交网络
题目链接:戳我 就是在floyd计算dis的时候,顺便把两点之间最短路的个数也计算了qwqwq \(sum[i][j]=\sum sum[i][k]*sum[k][j]\) 代码如下: #includ ...
- HAOI2010 订货
题目链接:戳我 费用流. 将每天分成早上和晚上两个点.源点向早上连容量INF,费用为进货量的边.早上向汇点连容量供货量,费用0.早上向晚上连容量为S,费用为0的边.晚上向第二天早上连容量S,费用0.之 ...
- UITableView编辑模式
UITableView有两种模式,普通模式和编辑模式.在编辑模式下可以对cell进行排序.删除.插入等等. 如何进入编辑模式 调用tableView的setEditing(editing: Bool, ...