Android拍照选取图片

Android经常会需要拍照、裁剪及图库中选择图片,其实都是通过intent调用系统相机或者系统图册,然后在onActivityResult中捕捉返回即可。

  • 正常拍照选择图片的代码:
例:        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);在onActivityResult中通过返回的intent.getExtras().get("data")便可以获取图片,但Android默认可用的应用内存大约为16M.所以Android为了不超出内存限制,在拍照返回时通过intent返回的bitmap会被压缩,这样导致一直都是获取的小图。所以在拍照时如果想返回大图需要通过Uri返回。#####拍照选择大图代码(tampUri为路径,开发者文档提供的代码):        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        // 指定调用相机拍照后照片的储存路径        intent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);在onActivityResult中通过tempUri进行下一步操作。说明:存储图片的路径建议使用 Activity.this.getExternalCacheDir()返回的/storage/sdcard0/Android/data//cache路径.通过路径获取 Uri 的方法,需要对不同版本兼容处理:7.0 以下系统: 使用 Uri.fromFile(new File(path)) 等即可.String fileName = "temp.jpg";File tmpFile = new File(this.getExternalCacheDir(),fileName);Uri tmpUri = Uri.fromFile(tmpFile);
  • 7.0 系统上,因为不允许传递通过 intent 传递 file:// 路径, 如果设置 targetSDK 为 24(7.0), 系统相机则无法获得 Uri.需要使用以下方法来获取 Uri.
    注意:AndroidManifest 中的 provider authorities 与 FileProvider.getUriForFile(); 方法中 authority 一致如:    String authority = "com.text.images";    FileProvider.getUriForFile(mContext, authority, filePath);                                            在 AndroidManifest.xml 中设置 provider:      在 res 目录下新建 xml 目录, 新建 provider_paths.xml 文件:这里我使用了外部存储 external-path, 具体标签参考 FileProvider .        <?xml version="1.0" encoding="utf-8"?>                            判断系统版本大于7.0时,采用 FileProvider 获取 Uri:        tring fileName = "temp.jpg";        File tmpFile = new File(this.getExternalCacheDir(),fileName);        tmpUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, tmpFile)为了防止Uri路径不对导致不能保存,使用前必须确保文件路径无误,不然会导致“无法保存剪裁的图片”的错误。或者无提示但不能点击确定保存图片。正确获取的Uri最终以file://开头常见问题:设置通过Uri返回后,onActivityResult中返回的intent为null。开发者文档中只说了简单调用的方法,其实可以添加一些其他属性。        intent.putExtra("return-data",false);        intent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString());        intent.putExtra("noFaceDetection", true);        不能添加intent.setType("image/*");,会因为找不到intent导致ActivityNotFoundException。        intent.putExtra("android.intent.extras.CAMERA_FACING",1);,系统默认开启的是后置摄像头,如果希望选择前置摄像头可以加这句。完整代码:    Intent openPhotoIntent;            if (Build.VERSION.SDK_INT < 19) {                openPhotoIntent = new Intent();                openPhotoIntent.setAction(Intent.ACTION_GET_CONTENT);                openPhotoIntent.setType(mFileTypes);            } else {                openPhotoIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);                openPhotoIntent.setType(mFileTypes);            }            return openPhotoIntent;            Intent openPhotoIntent;                    if (Build.VERSION.SDK_INT < 19) {                        openPhotoIntent = new Intent();                        openPhotoIntent.setAction(Intent.ACTION_GET_CONTENT);                        openPhotoIntent.setType(mFileTypes);                    } else {                        openPhotoIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);                        openPhotoIntent.setType(mFileTypes);                    }                    return openPhotoIntent;

更多相关文章

  1. Android系统应用调用Aidl
  2. Android 图片圆角的设置
  3. android 实用javamail发送邮件(非系统自带)
  4. Android - 小功能 - Android系统详解之获取图片和视频的缩略图
  5. android 图片平铺实现
  6. android流式布局、待办事项应用、贝塞尔曲线、MVP+Rxjava+Retrof
  7. Android操作系统简介

随机推荐

  1. SQLserver中字符串查找功能patindex和cha
  2. 将mater库中的系统存储过程批量生成*.sql
  3. SQLServer中字符串左对齐或右对齐显示的s
  4. SqlServer参数化查询之where in和like实
  5. SqlServer参数化查询之where in和like实
  6. Sql Server 查询性能优化之走出索引的误
  7. Sql Server查询性能优化之不可小觑的书签
  8. sqlserver 脚本和批处理指令小结
  9. SQL Server中通过扩展存储过程实现数据库
  10. sql server中批量插入与更新两种解决方案