How to pick image for crop from camera or gallery in Android 7.0?

后端 未结 9 2282
礼貌的吻别
礼貌的吻别 2020-12-01 04:56

Pick image for crop from gallery and camera it\'s done for below Android 7.0 but in Android Nought it crashes in camera. I use fileprovider for it but doesn\'t work.

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 05:36

    This worked for me like charm for Marshmallow as well as for 7.1.2 (Naugat)

    1. To pick image from camera, as described in this answer follow these steps from this blog:

    Write code as described in this step for both, Marshmallow as well as for 7.1.2 (Naugat)

    1. To crop the image, if you want to use intent com.android.camera.action.CROP (which is actually not recommanded but still many developer use), then you can call these methods with URI in old style. i.e. Uri.fromFile(file)

    cropIntent.setDataAndType(Uri.fromFile(file), "image/*");

    and

    cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));

    along with other methods (please search how to call intent com.android.camera.action.CROP)

    And, finally before calling startActivityForResult(cropIntent, CROP_ACTIVITY_CODE) make sure to write this this...

            if(Build.VERSION.SDK_INT>=24)
            {
                try
                {
                    Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
                    m.invoke(null);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
    

    That's it! I hope it helps someone.

提交回复
热议问题