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类和其他相关类实现相机功能,这种方法定制度比较高,洗染也比较复杂,一般平常的应用只 ...
随机推荐
- jmeter通过if控制器控制业务比例
以发帖,看帖,回帖三个事物为例,这里就10个用户跑10次,进行测试下: 可以看到看帖,回帖,发帖比例是5:3:2,先来说说怎么做到的,就是通过if控制器,分别来看下几个控制器的内容, 看帖(if控制器 ...
- 将UTC日期字符串转为本地时间字符串,如@"yyyy-MM-dd'T'HH:mm:ssZ"转换为本地时间
由于苹果商店上线应用24小时内会不稳定,更新提醒可能会陷入死循环,更新提醒需要24小时后弹出,需要把苹果返回的上线时间转换为本地时间故写了下边的方法: //将UTC日期字符串转为本地时间字符串//输入 ...
- Java中如何判断两个对象是否相等(Java equals and ==)
原文https://www.dutycode.com/post-140.html 如何判断两个对象相等,这个问题实际上可以看做是如何对equals方法和hashcode方法的理解. 从以下几个点来理解 ...
- 【BZOJ3196】【Luogu P3380】 【模板】二逼平衡树(树套树)
做数据结构一定要平\((fo)\)心\((de)\)静\((yi)\)气\((pi)\)...不然会四处出锅的\(QAQ\) 写法:线段树套平衡树,\(O(Nlog^3N)\).五个操作如果是对于整个 ...
- HTTP缓存剖析
web浏览器会自动缓存访问过的页面,当访问同一个页面的请求时,浏览器不再从服务器中重新下载页面而是优先使用本地缓存中的页面 为什么要进行web缓存 从用户的角度来看web缓存加快了上网速度,当然这是用 ...
- IPC 进程间通信方式——管道
进程间通信概述 数据传输:一个进程需要将它的数据发送给另一个进程,发送的数据量在一个字节到几兆字节之间 共享数据:多个进程想要操作共享数据,一个进程对共享数据的修改,别的进程应该立刻看到. 通知时间: ...
- 赋值语句C++(面宝P29)
; void main(){ int i=i; } 解析:main里面的i从声明的那一刻就是可见的了,所以main里的i不是1,而是一个未定义的值. 这段代码ctrl+F7编译能通过(有警告如下) c ...
- asp.net 怎么上传文件夹啊,不传压缩包!
ASP.NET上传文件用FileUpLoad就可以,但是对文件夹的操作却不能用FileUpLoad来实现. 下面这个示例便是使用ASP.NET来实现上传文件夹并对文件夹进行压缩以及解压. ASP.NE ...
- POJ 3683 神父赶婚宴 2-SAT+输出模板
题意:一个小镇里面只有一个牧师,现在有些新人要结婚,需要牧师分别去主持一个仪式,给出每对新人婚礼的开始时间 s 和结束时间 t ,还有他们俩的这个仪式需要的时间(每对新人需要的时间长短可能不同) d ...
- HDU 4725 The Shortest Path in Nya Graph (最短路 )
This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...