Skip to content Skip to sidebar Skip to footer

Android Crop Image From Gallery Nullpointexception

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

Solution 1:

  1. Select Action_Pick intent

                            Intent intent;
                            intent = newIntent(
                                    Intent.ACTION_PICK,
                                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            intent.setType("image/*");
                            Intentchooser= Intent.createChooser(intent,
                                    "Choose a Picture");
                            startActivityForResult(chooser,
                                    RequestCode.REQ_GALLERY);
    
  2. OnActivityResult get the image from URI

    case RequestCode.REQ_GALLERY:
            if (resultCode == Activity.RESULT_OK) {
                UriPhotoURI= data.getData();
                BitmapbitmapImage=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) {
    ByteArrayOutputStreambytes=newByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    Stringpath= MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
     }
    
  3. Apply crop operation

    privatevoiddoCrop(Uri mCurrentPhotoPath) {
    IntentcropIntent=newIntent("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 = newFile(AppConstants.BASE_FOLDER);
    
    if (!cameraFolder.exists()) {
        cameraFolder.mkdirs();
    }
    
    mSourceFileName = "/IMG_" + System.currentTimeMillis() + ".jpg";
    
    Filephoto=newFile(cameraFolder, mSourceFileName);
    try {
        photo.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    UrimCropImageUri= Uri.fromFile(photo);
    
    cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCropImageUri);
    
    startActivityForResult(cropIntent, RequestCode.REQ_CROP_IMG);
     }
    

Post a Comment for "Android Crop Image From Gallery Nullpointexception"