转载声明:Ryan的博客文章欢迎您的转载,但在转载的同时,请注明文章的来源出处,不胜感激! :-)
http://blog.csdn.net/floodingfire/article/details/8144615
在这篇博客中,我将向大家展示如何从相册截图。
上一篇博客中,我就拍照截图这一需求进行了详细的分析,试图让大家了解Android本身的限制,以及我们应当采取的实现方案。
根据我们的分析与总结,图片的来源有拍照和相册,而可采取的操作有
前面我们了解到,使用Bitmap有可能会导致图片过大,而不能返回实际大小的图片,我将采用大图Uri,小图Bitmap的数据存储方式。
我们将要使用到URI来保存拍照后的图片:
1 | private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg" ;//temp file |
2 | Uri imageUri = Uri.parse(IMAGE_FILE_LOCATION); |
不难知道,我们从相册选取图片的Action为Intent.ACTION_GET_CONTENT。
根据我们上一篇博客的分析,我准备好了两个实例的Intent。
一、从相册截大图:
01 | Intent intent = new Intent(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 ); |
13 | startActivityForResult(intent, CHOOSE_BIG_PICTURE); |
二、从相册截小图 01 | Intent intent = new Intent(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 ); |
12 | startActivityForResult(intent, CHOOSE_SMALL_PICTURE); |
三、对应的onActivityResult可以这样处理返回的数据 02 | case CHOOSE_BIG_PICTURE: |
03 |
Log.d(TAG, "CHOOSE_BIG_PICTURE: data = " + data); |
05 |
Bitmap bitmap = decodeUriAsBitmap(imageUri); |
06 |
imageView.setImageBitmap(bitmap); |
09 | case CHOOSE_SMALL_PICTURE: |
11 |
Bitmap bitmap = data.getParcelableExtra( "data" ); |
12 |
imageView.setImageBitmap(bitmap); |
14 |
Log.e(TAG, "CHOOSE_SMALL_PICTURE: data = " + data); |
01 | private Bitmap decodeUriAsBitmap(Uri uri){ |
04 |
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)); |
05 |
} catch (FileNotFoundException e) { |
效果图:
大图 | 小图 |
| |
基础篇:
【译】如何使用Android MediaStore裁剪大图片
上篇:
Android大图片裁剪终极解决方案(上:原理分析)
中篇:
Android大图片裁剪终极解决方案(中:从相册截图)
下篇:
Android大图片裁剪终极解决方案(下:拍照截图)
- Android 调用系统相机以及相册源码
- Android Fragment页打开相册
- Android 调用相册或相机选择图片
- Android跳转系统相机或相册获取图片
- Android中多层Fragment嵌套,调用相册返回Uri无法显示图片的问题解
- Android怎么从设置相册中的照片作背景(刚做的例子)
- android实现从相册和相机中取照片并剪辑
随机推荐
-
【Android】Android 多个APK数据共享
-
android httpclient基本用法
-
Android 应用程序签名、发布
-
获取Android SDK 源代码并在Eclipse中关
-
Android下基于XML的Graphics
-
Android AsyncTask理解及简单用法
-
图解YU12、I420、YV12、NV12、NV21、YUV4
-
Android数据存储方式:SharePreference、SQ
-
Android学习笔记: Android动画特效
-
BaseRecyclerViewAdapterHelper