转载声明:Ryan的博客文章欢迎您的转载,但在转载的同时,请注明文章的来源出处,不胜感激! :-)

http://blog.csdn.net/floodingfire/article/details/8144615


在这篇博客中,我将向大家展示如何从相册截图。

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

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

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

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

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

1 privatestaticfinalString IMAGE_FILE_LOCATION ="file:///sdcard/temp.jpg";//temp file
2 Uri imageUri = Uri.parse(IMAGE_FILE_LOCATION);//The Uri to store the big bitmap

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

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

一、从相册截大图:

01 Intent intent =newIntent(Intent.ACTION_GET_CONTENT,null);
02 intent.setType("image/*");
03 intent.putExtra("crop","true");
04 intent.putExtra("aspectX",2);
05 intent.putExtra("aspectY",1);
06 intent.putExtra("outputX",600);
07 intent.putExtra("outputY",300);
08 intent.putExtra("scale",true);
09 intent.putExtra("return-data",false);
10 intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
11 intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
12 intent.putExtra("noFaceDetection",true);// no face detection
13 startActivityForResult(intent, CHOOSE_BIG_PICTURE);
二、从相册截小图
01 Intent intent =newIntent(Intent.ACTION_GET_CONTENT,null);
02 intent.setType("image/*");
03 intent.putExtra("crop","true");
04 intent.putExtra("aspectX",2);
05 intent.putExtra("aspectY",1);
06 intent.putExtra("outputX",200);
07 intent.putExtra("outputY",100);
08 intent.putExtra("scale",true);
09 intent.putExtra("return-data",true);
10 intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
11 intent.putExtra("noFaceDetection",true);// no face detection
12 startActivityForResult(intent, CHOOSE_SMALL_PICTURE);
三、对应的onActivityResult可以这样处理返回的数据
01 switch(requestCode) {
02 caseCHOOSE_BIG_PICTURE:
03 Log.d(TAG,"CHOOSE_BIG_PICTURE: data = "+ data);//it seems to be null
04 if(imageUri !=null){
05 Bitmap bitmap = decodeUriAsBitmap(imageUri);//decode bitmap
06 imageView.setImageBitmap(bitmap);
07 }
08 break;
09 caseCHOOSE_SMALL_PICTURE:
10 if(data !=null){
11 Bitmap bitmap = data.getParcelableExtra("data");
12 imageView.setImageBitmap(bitmap);
13 }else{
14 Log.e(TAG,"CHOOSE_SMALL_PICTURE: data = "+ data);
15 }
16 break;
17 default:
18 break;
19 }
01 privateBitmap decodeUriAsBitmap(Uri uri){
02 Bitmap bitmap =null;
03 try{
04 bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
05 }catch(FileNotFoundException e) {
06 e.printStackTrace();
07 returnnull;
08 }
09 returnbitmap;
10 }

效果图:

大图 小图





基础篇:

【译】如何使用Android MediaStore裁剪大图片

上篇:

Android大图片裁剪终极解决方案(上:原理分析)

中篇:

Android大图片裁剪终极解决方案(中:从相册截图)

下篇:

Android大图片裁剪终极解决方案(下:拍照截图)

更多相关文章

  1. Android(安卓)USB Host的使用详解
  2. 【eoeAndroid社区索引】Android二维码知识汇总
  3. android Uri详解
  4. Android资源文件夹及资源文件的详细介绍
  5. Android中滑屏初探 ---- scrollTo 以及 scrollBy方法使用说明
  6. Google Android介绍..
  7. 把Android原生模拟器秒成渣的神器——Genymotion模拟器下载地址&
  8. Android(安卓)如何获取所有的wifi连接历史记录?
  9. 快速开发框架Afinal的使用(数据库操作,HTTP请求,网络图片加载,控件绑

随机推荐

  1. DevOps与传统的融合落地实践及案例分享
  2. Vue.js 基础入门系列(一)环境搭建
  3. 驱动器中的磁盘未被格式化. 不知道怎样寻
  4. Vue.js基础入门系列(二)数据绑定
  5. 中科三方:自签名的SSL证书与购买的专业SSL
  6. mysql数据库题目
  7. 智汇华云 | ArcherOS Stack共享存储虚拟
  8. Affinity Photo能代替PS的修图神器
  9. HTML5画布如何设置线的样式?
  10. 学Python编程有什么用?Python基础教程!