在这篇文章中,我将向大家展示如何从相册截图。
先看看效果图:

上一篇文章中,我就拍照截图这一需求进行了详细的分析,试图让大家了解Android本身的限制,以及我们应当采取的实现方案。大家可以回顾一下:Android实现拍照截图功能

根据我们的分析与总结,图片的来源有拍照和相册,而可采取的操作有

  • 使用Bitmap并返回数据
  • 使用Uri不返回数据

前面我们了解到,使用Bitmap有可能会导致图片过大,而不能返回实际大小的图片,我将采用大图Uri,小图Bitmap的数据存储方式。

我们将要使用到URI来保存拍照后的图片:

privatestatic final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";//temp fileUri imageUri = Uri.parse(IMAGE_FILE_LOCATION);//The Uri to store the big bitmap

不难知道,我们从相册选取图片的Action为Intent.ACTION_GET_CONTENT。

根据我们上一篇博客的分析,我准备好了两个实例的Intent。

一、从相册截大图:

Intent intent = newIntent(Intent.ACTION_GET_CONTENT, null);intent.setType("image/*");intent.putExtra("crop","true");intent.putExtra("aspectX",2);intent.putExtra("aspectY",1);intent.putExtra("outputX",600);intent.putExtra("outputY",300);intent.putExtra("scale",true);intent.putExtra("return-data",false);intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());intent.putExtra("noFaceDetection",true);// no face detectionstartActivityForResult(intent, CHOOSE_BIG_PICTURE);

二、从相册截小图

Intent intent = newIntent(Intent.ACTION_GET_CONTENT, null);intent.setType("image/*");intent.putExtra("crop","true");intent.putExtra("aspectX",2);intent.putExtra("aspectY",1);intent.putExtra("outputX",200);intent.putExtra("outputY",100);intent.putExtra("scale",true);intent.putExtra("return-data",true);intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());intent.putExtra("noFaceDetection",true);// no face detectionstartActivityForResult(intent, CHOOSE_SMALL_PICTURE);

三、对应的onActivityResult可以这样处理返回的数据

switch(requestCode) {caseCHOOSE_BIG_PICTURE:  Log.d(TAG,"CHOOSE_BIG_PICTURE: data = " + data);//it seems to be null  if(imageUri != null){    Bitmap bitmap = decodeUriAsBitmap(imageUri);//decode bitmap    imageView.setImageBitmap(bitmap);  }  break;caseCHOOSE_SMALL_PICTURE:  if(data != null){    Bitmap bitmap = data.getParcelableExtra("data");    imageView.setImageBitmap(bitmap);  }else{    Log.e(TAG,"CHOOSE_SMALL_PICTURE: data = " + data);  }  break;default:  break;}privateBitmap decodeUriAsBitmap(Uri uri){  Bitmap bitmap = null;  try{    bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));  }catch(FileNotFoundException e) {    e.printStackTrace();    returnnull;  }  returnbitmap;}

以上就是Android实现拍照截图功能的方法,希望对大家的学习有所帮助。

更多相关文章

  1. Android用户请注意,你的相机正在偷偷开启并拍照摄像!
  2. Android以当前Activity为基准进行截屏
  3. android studio 适配android7.0 android 6.0拍照调用系统裁剪工
  4. Android(安卓)系统拍照及打开系统相册 完美适配 Android(安卓)4
  5. 为什么说在Android中请求权限从来都不是一件简单的事情?
  6. Android全屏截图的方法,返回Bitmap并且保存在SD卡上
  7. 东拼西凑写的android 相机例子,包含一些遇到的坑
  8. android拍照上传到服务器端
  9. [置顶] 新修改ADB,支持Android(安卓)4.2 系统 ,全部中文命令,手机屏

随机推荐

  1. android sqlite 中文乱码。。麻烦详细点
  2. android 使用linux命令截屏
  3. android SAX
  4. javascript中的闭包原理以及数组与对象的
  5. 对象字面量的简化,推荐使用
  6. JS事件冒泡;常用字符串、数组函数
  7. Javascript中的函数和数据类型
  8. 函数与数据类型详解
  9. Arch Linux 安装 Anbox
  10. 配置香港高防虚拟主机必备要点