Android 相册、拍照选取图片后截图方案
2014-11-17 00:00
501 查看
摘要: 通过系统MediaStore可以对相册或者拍照获取的图片进行截图处理,但是这其中存在着诸多问题
一、相册方案
1、使用Bitmap并返回数据
2、使用Uri不返回数据
使用Bitmap有可能会导致图片过大,而不能返回实际大小的图片,我将采用大图Uri,小图Bitmap的数据存储方式。
我们将要使用到URI来保存拍照后的图片:
不难知道,我们从相册选取图片的Action为Intent.ACTION_GET_CONTENT。
一、从相册截大图:
二、从相册截小图
三、对应的onActivityResult可以这样处理返回的数据
二、拍照方案
拍照截图有点儿特殊,要知道,现在的Android智能手机的摄像头都是几百万的像素,拍出来的图片都是非常大的。因此,我们不能像对待相册截图一样使用Bitmap小图,无论大图小图都统一使用Uri进行操作。
一、首先准备好需要使用到的Uri:
二、使用MediaStore.ACTION_IMAGE_CAPTURE可以轻松调用Camera程序进行拍照:
三、接下来就可以在 onActivityResult中拿到返回的数据(Uri),并将Uri传递给截图的程序。
可以看到,无论是拍大图片还是小图片,都是使用的Uri,只是尺寸不同而已。我们将这个操作封装在一个方法里面。
四、最后一步,我们已经将数据传入裁剪图片程序,接下来要做的就是处理返回的数据了:
感谢 floodingfire 的方案分享,想看更详细的请走:http://blog.csdn.net/floodingfire/article/details/8144587
一、相册方案
1、使用Bitmap并返回数据
2、使用Uri不返回数据
使用Bitmap有可能会导致图片过大,而不能返回实际大小的图片,我将采用大图Uri,小图Bitmap的数据存储方式。
我们将要使用到URI来保存拍照后的图片:
private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";//temp file Uri imageUri = Uri.parse(IMAGE_FILE_LOCATION);//The Uri to store the big bitmap
不难知道,我们从相册选取图片的Action为Intent.ACTION_GET_CONTENT。
一、从相册截大图:
Intent intent = new Intent(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 detection startActivityForResult(intent, CHOOSE_BIG_PICTURE);
二、从相册截小图
Intent intent = new Intent(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 detection startActivityForResult(intent, CHOOSE_SMALL_PICTURE);
三、对应的onActivityResult可以这样处理返回的数据
switch (requestCode) { case CHOOSE_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; case CHOOSE_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; } private Bitmap decodeUriAsBitmap(Uri uri){ Bitmap bitmap = null; try { bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)); } catch (FileNotFoundException e) { e.printStackTrace(); return null; } return bitmap; }
二、拍照方案
拍照截图有点儿特殊,要知道,现在的Android智能手机的摄像头都是几百万的像素,拍出来的图片都是非常大的。因此,我们不能像对待相册截图一样使用Bitmap小图,无论大图小图都统一使用Uri进行操作。
一、首先准备好需要使用到的Uri:
private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";//temp file Uri imageUri = Uri.parse(IMAGE_FILE_LOCATION);//The Uri to store the big bitmap
二、使用MediaStore.ACTION_IMAGE_CAPTURE可以轻松调用Camera程序进行拍照:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//action is capture intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, TAKE_BIG_PICTURE);//or TAKE_SMALL_PICTURE
三、接下来就可以在 onActivityResult中拿到返回的数据(Uri),并将Uri传递给截图的程序。
switch (requestCode) { case TAKE_BIG_PICTURE: Log.d(TAG, "TAKE_BIG_PICTURE: data = " + data);//it seems to be null cropImageUri(imageUri, 800, 400, CROP_BIG_PICTURE) break; case TAKE_SMALL_PICTURE: Log.i(TAG, "TAKE_SMALL_PICTURE: data = " + data); cropImageUri(imageUri, 300, 150, CROP_SMALL_PICTURE); break; default: break; }
可以看到,无论是拍大图片还是小图片,都是使用的Uri,只是尺寸不同而已。我们将这个操作封装在一个方法里面。
private void cropImageUri(Uri uri, int outputX, int outputY, int requestCode){ Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 2); intent.putExtra("aspectY", 1); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); intent.putExtra("return-data", false); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, requestCode); }
四、最后一步,我们已经将数据传入裁剪图片程序,接下来要做的就是处理返回的数据了:
switch (requestCode) { case CROP_BIG_PICTURE://from crop_big_picture Log.d(TAG, "CROP_BIG_PICTURE: data = " + data);//it seems to be null if(imageUri != null){ Bitmap bitmap = decodeUriAsBitmap(imageUri); imageView.setImageBitmap(bitmap); } break; case CROP_SMALL_PICTURE: if(imageUri != null){ Bitmap bitmap = decodeUriAsBitmap(imageUri); imageView.setImageBitmap(bitmap); }else{ Log.e(TAG, "CROP_SMALL_PICTURE: data = " + data); } break; default: break; }
感谢 floodingfire 的方案分享,想看更详细的请走:http://blog.csdn.net/floodingfire/article/details/8144587
相关文章推荐
- Android设置头像,手机拍照或从本地相册选取图片作为头像
- Android设置头像,手机拍照或从本地相册选取图片作为头像
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- Android 拍照与从相册选取图片时,图片不显示的问题
- android 4.3相册选取截图“无法加载图片”问题
- Android手机拍照和手机相册选取图片的工具类ImageUtils
- Android通过手机拍照或从本地相册选取图片设置头像
- Android设置头像,手机拍照或从本地相册选取图片作为头像
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时为什么会回到home界面或者到前一个界面,然后才会跳转到当前activity
- Android实现选择相册图片和拍照,并手动截图返回图片路径和Bitmap对象
- Android 拍照或从本地相册选取图片
- Android启动相机拍照、相册选取图片,裁剪图片
- unity3d 保存(截图、拍照)图片 到Android IPhone手机相册
- Android图片压缩(包含拍照或从相册选取图片,PopupWindow的使用)
- android 打开默认的系统相册,或拍照选取图片,并图片裁剪;
- android 调用系统相册选取照片或者打开相机拍照获取图片
- Android之拍照和相册选取图片并裁剪得到路径
- Android之拍照和相册选取图片并裁剪得到路径
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- Android之本地相册图片选取和拍照以及图片剪辑