应用程序崩溃后拍照并将结果传递给onActivityResult

时间:2017-09-14 11:41:22

标签: java android

我试图通过调用相机意图来拍照并在图像视图中显示它。我的代码几乎适用于所有设备,除了一些带有kitkat版本的MI设备。这是我的代码

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

以下是某些设备中抛出的异常

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

请有人帮我解决这个问题。它适用于其他所有设备。

2 个答案:

答案 0 :(得分:0)

它是MI设备安全问题。您无法获得所选文件的直接路径它们只会提供内容提供商Url 我建议你使用这个Coomar Picker Library。我也被困在同一个问题上这么多天了。这是我找到它的唯一解决方案。这样可以节省你的时间

以下是它的维基WIKI

答案 1 :(得分:0)

我认为当您调用startActivityForResult(intent)方法时,您的imageUri变为null,您可以通过转到开发人员选项来启用“不保留活动”选项来解决此问题,从而在其他设备上复制该问题。覆盖活动中的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"));
    }
}