我在Camera.i工作。想要从我的画廊中选择图像并裁剪所选照片然后在我的imageview.i中显示它写了一些代码,但我在裁剪时遇到问题。 这是我的错误
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();
}
}
}
这是我的代码 我在裁剪图像时遇到问题,因为当我删除裁剪我的应用程序工作完美 我究竟做错了什么?如果有人知道解决方案,请帮助我
答案 0 :(得分:2)
选择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);
}