简体   繁体   English

拍照后并将结果传送到onActivityResult时,应用程序崩溃

[英]App crashing after taking picture and while delivering the result to onActivityResult

I am trying to take a picture by calling camera intent and show it in a image view. 我试图通过调用相机意图拍摄照片并将其显示在图像视图中。 My code works on almost all devices except on some MI Devices with kitkat version. 我的代码几乎可以在所有设备上运行,除了某些带有kitkat版本的MI设备上。 Here is my code 这是我的代码

     private void initializeCameraIntent() {
            Permiso.getInstance().requestPermissions(new Permiso.IOnPermissionResult() {
           /* if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
                    == PackageManager.PERMISSION_DENIED){*/
                @Override
                public void onPermissionResult(Permiso.ResultSet resultSet) {
                    if (resultSet.areAllPermissionsGranted()) {
                        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        File photoFile = null;
                        try {
                            photoFile = createImageFile();
                        } catch (IOException ex) {
                            Log.e(TAG, "onTileSelected: ", ex);
                        }
                        if (photoFile != null) {
                            Uri uri = Uri.fromFile(photoFile);
                            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                            imageUri = uri.toString();
                            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
                        }
                    } else {
                        Toast.makeText(MessagesFragment.this.getActivity(),
                                getString(R.string.msg_permission_required), Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onRationaleRequested(Permiso.IOnRationaleProvided callback, String... permissions) {
                    Permiso.getInstance().showRationaleInDialog(null,
                            getString(R.string.msg_permission_required),
                            null, callback);
                }
            }, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
 protected File createImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
        String imageFileName = "IMG_" + timeStamp;
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        if (!storageDir.exists()) {
            if (!storageDir.mkdirs()) {
                return null;
            }
        }
        return File.createTempFile(imageFileName, ".jpg", storageDir);
    }

 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_IMAGE_CAPTURE:
                    if (!imageUri.isEmpty()) {
                        showSendImageFragment(imageUri);
                        imageUri = "";
                    }
                    break;
}

And below is the exception thrown in some devices 以下是某些设备抛出的异常

Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=null} to activity {com.xxx.xxx/com.xxx.xxx.view.activity.MainActivity}: java.lang.NullPointerException

Please Someone help me fixing this. 请有人帮我解决这个问题。 It works on every Other device. 它可以在其他所有设备上使用。

Its a MI devices Security issue.You wont be able to get the direct path of your selected file They will just provide only the content provider Url I suggested you use this Coomar Picker Library .I also got stuck on the same issue for so many days.This is the only solution i have found it.This will save your time 它是一个MI设备的安全问题。您将无法获得所选文件的直接路径。它们将仅提供内容提供者网址。我建议您使用此Coomar Picker库 。我也因为同样的问题而停留了很多天这是我找到的唯一解决方案。这将节省您的时间

And Here is the wiki for it WIKI 这里是它的维基WIKI

I think your imageUri is becoming null when your are calling startActivityForResult(intent) method you can replicate the issue on other devices by going to developer options enabling do not keep activities option to overcome this issue. 我认为当您调用startActivityForResult(intent)方法时,您的imageUri会变为null,您可以通过转到开发人员选项启用不保留活动选项来克服此问题,从而在其他设备上复制该问题。 Override onSaveInstanceState and onRestoreInstanceState methods in your activity 在您的活动中覆盖onSaveInstanceStateonRestoreInstanceState方法

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if (imageUri != null){
        outState.putString("cameraImageUri", imageUri.toString());
    }
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if (savedInstanceState.containsKey("cameraImageUri")){
        imageUri = Uri.parse(savedInstanceState.getString("imageUri"));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM