上一篇文章中,我就拍照截图这一需求进行了详细的分析,试图让大家了解Android本身的限制,以及我们应当采取的实现方案。大家可以回顾一下:Android实现拍照截图功能
根据我们的分析与总结,图片的来源有拍照和相册,而可采取的操作有
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 = 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 nullif(imageUri != null){Bitmap bitmap = decodeUriAsBitmap(imageUri);//decode bitmapimageView.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实现拍照截图功能的方法,希望对大家的学习有所帮助。