Android 调用相机、相册功能
清单文件中增加对应权限,动态申请权限(此部分请参考Android 动态申请权限,在此不作为重点描述)
private static final int REQUEST_CODE_ALBUM = 100;//打开相册
private static final int REQUEST_CODE_CAMERA = 101;//打开相机
//调用相册
private void openAlbum(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_CODE_ALBUM);
}
//调用相机
private void openCamera1(){
Intent intent;
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CODE_CAMERA);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE_ALBUM && resultCode == RESULT_OK){
if (data != null) {
// 照片的原始资源地址
Uri uri = data.getData();
String path = uri.getPath();
ContentResolver cr = context.getContentResolver();
try {
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
/* 将Bitmap设定到ImageView */
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Log.e("Exception", e.getMessage(), e);
}
}
}else if(requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK){
if(data != null && data.getData() != null){
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap bitmap = extras.getParcelable("data");
/* 将Bitmap设定到ImageView */
imageView.setImageBitmap(bitmap);
//可将图片保存下来,用于上传或其他操作(如果不需要可以省略此步)
String path = savePicToSdcard(image,Environment.getExternalStorageDirectory().getPath(),System.currentTimeMillis() + ".jpg");
}
}
}
}
public static String savePicToSdcard(Bitmap bitmap, String path, String fileName) {
String filePath = "";
if (bitmap != null) {
filePath = path + File.separator + fileName;
File destFile = new File(filePath);
OutputStream os = null;
try {
os = new FileOutputStream(destFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
os.close();
} catch (IOException e) {
filePath = "";
}
}
return filePath;
}
上述相机方法相片清晰度低,获取的是返回对象中的略缩图;如对照片清晰度有要求,可以在上面方法的基础上进行修改,方法如下: 需要在清单文件中application中增加如下代码:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider> 创建file_paths.xml文件 项目目录res下创建xml文件夹,xml文件夹下创建file_paths.xml文件,文件内容:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_storage_root"
path="." />
</paths> 配置完成后,修改增加如下代码:
private static final int REQUEST_RESULT_CODE = 102;//裁剪后保存
//调用相机(指定相机拍摄照片保存地址,相片清晰度高)
private void openCamera2(){
String photoPath = Environment.getExternalStorageDirectory().getPath()+"/"+File.separator+"123.jpg";
File pictureFile = new File(photoPath);
Uri picUri;
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String packageName = context.getApplicationContext().getPackageName();
picUri = FileProvider.getUriForFile(context, new StringBuilder(packageName).append(".fileprovider").toString(), pictureFile);
} else {
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
picUri = Uri.fromFile(pictureFile);
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
startActivityForResult(intent, REQUEST_CODE_CAMERA);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE_ALBUM && resultCode == RESULT_OK){
if (data != null) {
// 照片的原始资源地址
Uri uri = data.getData();
String path = uri.getPath();
ContentResolver cr = context.getContentResolver();
try {
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
/* 将Bitmap设定到ImageView */
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Log.e("Exception", e.getMessage(), e);
}
}
}else if(requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK){
File tempFile = new File(photoPath);
cropImg(getImageContentUri(tempFile));//对照片进行裁剪保存
}else if(requestCode ==REQUEST_RESULT_CODE && resultCode == RESULT_OK){
try {
Bitmap image = BitmapFactory.decodeStream(getContentResolver().openInputStream(mUriPath));
/* 将Bitmap设定到ImageView */
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
public Uri getImageContentUri(File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Images.Media._ID},
MediaStore.Images.Media.DATA + "=? ",
new String[]{filePath}, null); if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor
.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
if (imageFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
} public void cropImg(Uri uri){
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*"); //实现对图片的裁剪,必须要设置图片的属性和大小
intent.putExtra("crop", "true"); //滑动选中图片区域
intent.putExtra("aspectX", 1); //裁剪框比例1:1
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 700); //输出图片大小
intent.putExtra("outputY", 700);
intent.putExtra("return-data", true); //有返回值 String mLinshi = System.currentTimeMillis() + ".jpg";
photoFile = new File(Environment.getExternalStorageDirectory().getPath(), mLinshi); mUriPath = Uri.parse("file://" + photoFile.getAbsolutePath());
//将裁剪好的图输出到所建文件中
intent.putExtra(MediaStore.EXTRA_OUTPUT, mUriPath);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
//注意:此处应设置return-data为false,如果设置为true,是直接返回bitmap格式的数据,耗费内存。设置为false,然后,设置裁剪完之后保存的路径,即:intent.putExtra(MediaStore.EXTRA_OUTPUT, uriPath);
intent.putExtra("return-data", false);
startActivityForResult(intent, REQUEST_RESULT_CODE);
}
Android 调用相机、相册功能的更多相关文章
- Android简单调用相机Camera功能,实现打开照相功能
在最開始接触Android相机功能之前,先来体验一下Android调用系统照相功能吧 核心代码 Intent intent = new Intent(); //调用照相机 intent.setActi ...
- Android调用相机拍摄照片并显示到 ImageView控件中
在前面的一篇文章中曾介绍过简单的开启相机照相功能,详见 Android简单调用相机Camera功能,实现打开照相功能 ,这一次就会将前面拍摄的照片显示到ImageView中,形成一个完整的效果 看实例 ...
- Android调用系统相册和拍照的Demo
最近我在群里看到有好几个人在交流说现在网上的一些Android调用系统相册和拍照的demo都有bug,有问题,没有一个完整的.确实是,我记得一个月前,我一同学也遇到了这样的问题,在低版本的系统中没问题 ...
- android 调用相机拍照及相册
调用系统相机拍照: private Button btnDyxj; private ImageView img1; private File tempFile; btnDyxj = (Button) ...
- Android调用相机实现拍照并裁剪图片,调用手机中的相冊图片并裁剪图片
在 Android应用中,非常多时候我们须要实现上传图片,或者直接调用手机上的拍照功能拍照处理然后直接显示并上传功能,以下将讲述调用相机拍照处理图片然后显示和调用手机相冊中的图片处理然后显示的功能,要 ...
- Xamarin.Android 调用本地相册
调用本地相册选中照片在ImageView上显示 代码: using System; using System.Collections.Generic; using System.Linq; using ...
- Xamarin.Android 调用手机拍照功能
最近开发Android遇到了调用本地拍照功能,于是在网上搜了一些方法,加上自己理解的注释,在这儿记录下来省的下次用时候找不到,同事也给正在寻找调用本地拍照功能的小伙伴一些帮助~ 实现思路:首先加载-- ...
- Android调用相机拍照并返回路径和调用系统图库选择图片
调用系统图库: Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI); ...
- Android调用相机并将照片存储到sd卡上
Android中实现拍照有两种方法,一种是调用系统自带的相机,然后使用其返回的照片数据. 还有一种是自己用Camera类和其他相关类实现相机功能,这种方法定制度比较高,洗染也比较复杂,一般平常的应用只 ...
随机推荐
- python-装饰器案例1
python-装饰器案例1 高阶函数+嵌套函数=装饰器 例1: import time def timer(func): def deco(): start_time=time.time() func ...
- maven模块开发(转)
所有用Maven管理的真实的项目都应该是分模块的,每个模块都对应着一个pom.xml.它们之间通过继承和聚合(也称作多模块,multi-module)相互关联.那么,为什么要这么做呢?我们明明在开发一 ...
- 网络编程与socket
.互联网协议 互联网协议又称为网络七层协议,OSI七层协议,OSI是一个世界标准组织. OSI七层协议: - 应用层 - 表示层 - 会话层 - 传输层 - 网络层 - 数据链路层 - 物理连接层 学 ...
- P1801 黑匣子[对顶堆]
没错我就是专门找对顶堆练习题的.现在感觉对顶堆使用面有点狭窄.这道题由于我询问是随时间单调增的,而且数据比较友好,应该是插入几次就询问一下的.而中位数那题也是经常询问的.如果查询的东西不单调,或者查询 ...
- 数组转list
例如String数组转成Integer泛型的List String [] pathArr = deptPath.split(","); Lit<Integer> dep ...
- 严重: Servlet [SelectController] in web application [/servlet4] threw load() exception
在web.xml路径配置.jar包导入都正确的情况下,那就考虑是环境问题. 1.servers-->clean 将代码从tomcat中清除 2.Project-->clean 将ecli ...
- vue - router + iView 的使用(简单例子)
所使用的工具:谷歌浏览器.Nodejs(自带npm).HBuilder 0.要先安装Nodejs,下载安装即可 0-1.安装vue-cli,打开cmd 输入 npm install -g @vue/c ...
- MySQL--关于MySQL练习过程中遇到的AVG()函数处理空值的问题
最近正准备面试,所以本来不怎么熟悉的SQL语句迫切需要练习,学习一下 在此感谢 笨鸟先飞-天道酬勤 大佬的博客:https://blog.csdn.net/dehu_zhou/article/deta ...
- python-加密算法
#!/usr/bin/python3 # coding:utf-8 # Auther:AlphaPanda # Description: 使用hashlib模块的md5和sha系列加密算法对字符串进行 ...
- javascript中的原型和原型链(三)
1. 图解原型链 1.1 “铁三角关系”(重点) function Person() {}; var p = new Person(); 这个图描述了构造函数,实例对象和原型三者之间的关系,是原型链的 ...