来自库nullpointexception的android裁剪图像 [英] android crop image from gallery nullpointexception

查看:137
本文介绍了来自库nullpointexception的android裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Camera.i中工作,我想从图库中选择图像并裁剪所选照片,然后在imageview中显示.我编写了一些代码,但是裁剪时遇到问题. 这是我的错误

I'm working in Camera.i want to choose image from my gallery and crop selected photo and then show it in my imageview.i wrote some code but i have problem in cropping. this is a my error

 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();
        }
    }
}

这是我的代码 我在裁剪图片时遇到问题,因为当我删除裁剪时,我的应用程序运行正常 我究竟做错了什么?如果有人知道解决方案,请帮助我

this is a my code I have problem in Cropping image because when i removed crop my app working perfect what am i doing wrong? if anyone knows solution please help me

推荐答案

  1. 选择动作选择意图

  1. Select Action_Pick intent

                        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获取图像

  • OnActivityResult get the image from 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);
     }
    

  • 应用裁剪操作

  • Apply crop operation

    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);
     }
    

  • 这篇关于来自库nullpointexception的android裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆