Intent Camera崩溃了应用程序

时间:2015-07-04 12:46:28

标签: android camera crash

我正在使用以下代码捕获它在除Android 5.0.1之外的所有Android版本中正常工作的图像。请帮助检测代码中的错误。我还添加了应用程序所需的所有权限。

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);

和onActivityResult如下

private static File getOutputMediaFile(int type) {

    // External sdcard location
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            ""+R.string.folder_name);

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {

            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");
    } else if (type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}


public Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if the result is capturing Image
    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
        if (resultCode == -1) {
            // successfully captured the image
            // display it in image view
             BitmapFactory.Options options = new BitmapFactory.Options();

             // downsizing image as it throws OutOfMemory Exception for larger
             // images
             options.inSampleSize = 8;

             final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                     options);

             createComplaint.setPhotoBitmap(bitmap);
                imgVPicPhoto.setImageBitmap(bitmap);

        } else if (resultCode == 0) {
            // user cancelled Image capture

        } else {
            // failed to capture image

        }
    }
}

Logcat错误消息 -

07-04 18:25:47.513: E/AndroidRuntime(13968): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getPath()' on a null object reference

07-04 18:25:47.513:E / AndroidRuntime(13968):at com.myaccessibility.newcomplaint.NewPhotoFragment.onActivityResult(NewPhotoFragment.java:379) 07-04 18:25:47.513:E / AndroidRuntime(13968):在android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:167) 07-04 18:25:47.513:E / AndroidRuntime(13968):at com.myaccessibility1.MyAccessibility.onActivityResult(MyAccessibility.java:734) 07-04 18:25:47.513:E / AndroidRuntime(13968):在android.app.Activity.dispatchActivityResult(Activity.java:6543) 07-04 18:25:47.513:E / AndroidRuntime(13968):在android.app.ActivityThread.deliverResults(ActivityThread.java:4054)

1 个答案:

答案 0 :(得分:0)

fileUrinull

请注意,当您在后台时,您的流程可能会被终止。这包括您的应用要求其他应用拍摄照片的时间。您需要确保在重新启动进程时可以重建环境。例如,可能需要将fileUri放入提供给Bundle的{​​{1}},然后从提供给onSaveInstanceState()的{​​{1}}恢复。您应该这样做以处理配置更改,例如用户旋转屏幕。

您可以在文档中详细了解the process modelhandling configuration changes