android 6.0,Device Nexus Galaxy S.
拍照
裁剪照片
将图像设置为ImageView
方法裁剪照片:
private void performCrop(Intent data) {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(Uri.fromFile(myPhotoFile), "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", ImageFactory.getAvatarWidth());
cropIntent.putExtra("outputY", ImageFactory.getAvatarHeight());
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
}
myPhotoFile :/ storage / emulated / 0 / myphoto_2383996e1fd8210 / myphoto_1487940877714
大小:2062768字节。
获得结果:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == PIC_CROP) {
onActivityResultPicCrop(resultCode, data);
}
}
private void onActivityResultPicCrop(int resultCode, Intent data) {
if (data != null) {
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap selectedImage = extras.getParcelable("data");
Bitmap.Config config = selectedImage.getConfig();
ImageView imageView = (ImageView) layoutService.rootView
.findViewById(md.qsystems.android.profileapp.R.id.imageViewAvatar);
imageView.setImageBitmap(selectedImage);
updateAvatar(false);
}
}
但是“extras”为NULL,因此抛出NPE。
在Android 4.4(KitKat)上它运行良好,但在Android 6.0上没有。
答案 0 :(得分:0)
Extra为null,因为您的意图可能无法裁剪。考虑 为返回代码添加测试。
@Override public void onActivityResult ( int requestCode, int resultCode, Intent data){ if (requestCode == REQUEST_IMAGE_CROP && resultCode == RESULT_OK) { onActivityResultPicCrop(resultCode, data); } }
我的代码:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == PIC_CROP) {
onActivityResultPicCrop(resultCode, data);
}
}
但它没有帮助。我得到了相同的NPE。