android从图库中裁剪图像nullpointerexception
Posted
技术标签:
【中文标题】android从图库中裁剪图像nullpointerexception【英文标题】:android crop image from gallery nullpointexception 【发布时间】:2016-02-17 11:25:54 【问题描述】:我正在使用相机。我想从我的图库中选择图像并裁剪选定的照片,然后将其显示在我的 imageview 中。我编写了一些代码,但我在裁剪时遇到了问题。 这是我的错误
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
private void CropPictureFromGallery()
Intent pickImageIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickImageIntent.setType("image/*");
pickImageIntent.putExtra("crop", "true");
pickImageIntent.putExtra("outputX", 200);
pickImageIntent.putExtra("outputY", 200);
pickImageIntent.putExtra("aspectX", 1);
pickImageIntent.putExtra("aspectY", 1);
pickImageIntent.putExtra("scale", true);
pickImageIntent.putExtra("outputFormat",
startActivityForResult(pickImageIntent, PICK_FROM_GALLERY);
public void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK)
Uri targetUri = data.getData();
Bitmap bitmap;
try
bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(targetUri));
mAvatar.setImageBitmap(bitmap);
catch (FileNotFoundException e)
// TODO Auto-generated catch block
e.printStackTrace();
这是我的代码 我在裁剪图像时遇到问题,因为当我删除裁剪时,我的应用程序运行良好 我究竟做错了什么?如果有人知道解决方案,请帮助我
【问题讨论】:
首先通过调试Uri targetUri = data.getData();
来检查你在targetUri
中得到了什么,如果它变得空,你就没有得到裁剪的图像Uri。然后在AsyncTask
中执行Bitmap Decoding
操作,因为这是一个昂贵的过程。
【参考方案1】:
选择 Action_Pick 意图
Intent intent;
intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
Intent chooser = Intent.createChooser(intent,
"Choose a Picture");
startActivityForResult(chooser,
RequestCode.REQ_GALLERY);
OnActivityResult 从 URI 中获取图片
case RequestCode.REQ_GALLERY:
if (resultCode == Activity.RESULT_OK)
Uri PhotoURI = data.getData();
Bitmap bitmapImage = null;
try
bitmapImage = decodeBitmap(PhotoURI);
BitmapFactory.decodeStream(getCurrActivity().getContentResolver().openInputStream(PhotoURI));
doCrop(getImageUri(getCurrActivity(), bitmapImage));
catch (FileNotFoundException e)
e.printStackTrace();
break;
public Uri getImageUri(Context inContext, Bitmap inImage)
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
应用裁剪操作
private void doCrop(Uri mCurrentPhotoPath)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(mCurrentPhotoPath, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 320);
cropIntent.putExtra("outputY", 320);
File cameraFolder;
cameraFolder = new File(AppConstants.BASE_FOLDER);
if (!cameraFolder.exists())
cameraFolder.mkdirs();
mSourceFileName = "/IMG_" + System.currentTimeMillis() + ".jpg";
File photo = new File(cameraFolder, mSourceFileName);
try
photo.createNewFile();
catch (IOException e)
e.printStackTrace();
Uri mCropImageUri = Uri.fromFile(photo);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCropImageUri);
startActivityForResult(cropIntent, RequestCode.REQ_CROP_IMG);
【讨论】:
这是一个完美的例子,但你能告诉我我的代码有什么问题吗?以上是关于android从图库中裁剪图像nullpointerexception的主要内容,如果未能解决你的问题,请参考以下文章
android:从图库中选择图像,然后裁剪并显示在图像视图中