Null pointer Exception on file- URI?

后端 未结 4 1150
感动是毒
感动是毒 2020-12-11 05:19

In my app a capture button is there to capture an image using device camera,so I have used a method captureImage() on the click event of that button.When I click the button

4条回答
  •  庸人自扰
    2020-12-11 06:07

    In my case, I found out SDK version 23 and up might need runtime permission for writing external storage. So I solved like this:

    public Uri getOutputMediaFileUri(int type) {
            requestRuntimePermission();
            return Uri.fromFile(getOutputMediaFile(type));
        }
    
    public void requestRuntimePermission() {
            if (Build.VERSION.SDK_INT >= 23) {
                if (ContextCompat.checkSelfPermission(context,android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(activity,
                            new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                }
            }
        }
    

    Note: You might put this instead of context and activity above if applicable

提交回复
热议问题