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/*"/> 调用打开摄像头后,聚焦后拍照,点击确认,这时页面会出现刷新动作,然后回退 ...
随机推荐
- [LeetCode 题解]: Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- django 快捷代码提示
1.右键项目,Mark Directory As Source Root 2.settings配置 3.import包时可忽略app名了
- Page.FindControl(string id) 与母版页结合后发现的一个问题
MSDN上解释Page.FindControl(string id)方法用于查找指定ID的控件.例如: <asp:TextBox id="Email" runat=" ...
- python3 的zip()函数
重点 https://blog.csdn.net/qq826364410/article/details/78259796 为啥会出现几个两个空列表????
- gitlab迁移版本库(保留原版本库的所有内容)
如果你想从别的 Git 托管服务那里复制一份源代码到新的 Git 托管服务器上的话,可以通过以下步骤来操作. 1) 从原地址克隆一份裸版本库,比如原本托管于 GitHub git clone --ba ...
- 【maven】---初识
前言 最近在研究自动化测试,其中的一个研究点儿就是maven,去download了一本书,看了看.下面是自己的从书中摘录的一些关于maven的概念性的东西. 内容 maven是什么? Maven是一个 ...
- 从哈希结构去理解PHP数组
php的数组实际上就是hash_table,无论是 数字索引数组array(1, 2, 3) 还是关联数组array(1 => 2, 2=> 4)等等. 一,这里的hash_table有几 ...
- P4175 [CTSC2008]网络管理 树剖+树套树
$ \color{#0066ff}{ 题目描述 }$ M公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门.为了让分布在世界各地的N个部门之间协同工作,公司搭建了一个连接整个公司的通 ...
- js数组的常用操作
数组合并 var arr=[1,"abc","张三","122"]; var b=["今天天气不错","适合学 ...
- lexical or preprocessor issue file not found in Xcode
The very last suggestion is all I needed to fix this issue. Close & re-open Xcode.