前几天做项目中选择图片的过程中遇到高版本和小米手机出现无法选择和崩溃的问题,现在记录下来,后面出现同类问题,也好查找
1,定义常量:
private static final int TAKE_PICTURE = 3; private static final int CHOOSE_PICTURE = 4; private static final int CROP_SMALL_PICTURE = 5;
2,拍照,选择相册图片逻辑,下面红色部分就是兼容高版本和低版本选取相册图片的代码,不会出现问题:
public void showPicturePicker(Context context) { final String[] stringItems = { "拍照", "相册" }; final ActionSheetDialog dialog = new ActionSheetDialog(this, stringItems, null); dialog.isTitleShow(false).show(); dialog.setOnOperItemClickL(new OnOperItemClickL() { @Override public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) { switch (position) { case 0: Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 指定照片保存路径(SD卡),image.jpg为一个临时文件,每次拍照后这个图片都会被替换 openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(openCameraIntent, TAKE_PICTURE); dialog.dismiss(); break; case 1: Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*"); startActivityForResult(intent, CHOOSE_PICTURE); dialog.dismiss(); break; default: break; } } }); }
3,onActivityforResult里面的逻辑代码:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); String absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath(); String headpic = absolutePath + File.separator + "headpic.png"; switch (requestCode) { case Constant.TO_CHANGENAMEACTIVITY_REQUESTCODE: if (data != null) { String name = data.getStringExtra("name").trim(); if (!isNull(name)) { tv_mine_name_content.setText(name); } } break; case TAKE_PICTURE: cropImageUri(imageUri, 154, 154, CROP_SMALL_PICTURE); break; case CHOOSE_PICTURE: if (data != null) { // 照片的原始资源地址 Uri originalUri = data.getData(); cropImageUri(originalUri, 154, 154, CROP_SMALL_PICTURE); } break; case CROP_SMALL_PICTURE: if(imageUri != null){ Bitmap bitmap; try { bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uritempFile)); ImageTools.savePhotoToSDCard(bitmap, absolutePath, "headpic.png"); uploadHeadPic(headpic); bitmap.recycle(); } catch (FileNotFoundException e) { toast("找不到图片文件"); e.printStackTrace(); } } break; default: break; } }
注意uritempFile ,必须如下写,否则在小米手机上肯定崩溃:
Uri uritempFile = Uri.parse("file://" + "/" + Environment.getExternalStorageDirectory().getPath() + "/" + "small.jpg");
上面有调用裁剪图片的调用,代码如下:
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", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); // intent.putExtra("return-data", false); intent.putExtra(MediaStore.EXTRA_OUTPUT, uritempFile); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, requestCode); }