1

I am using the following code to capture the image it is working fine in the all version of android except Android 5.0.1. Please help to detect the error in the code. I have also add all the permissions that required by the app.

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

and onActivityResult as below

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 Error Message -

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): at 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): at android.app.Activity.dispatchActivityResult(Activity.java:6543) 07-04 18:25:47.513: E/AndroidRuntime(13968): at android.app.ActivityThread.deliverResults(ActivityThread.java:4054)

  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Jul 04 '15 at 12:50
  • possible duplicate of [Android startCamera gives me null Intent and ... does it destroy my global variable?](http://stackoverflow.com/questions/20424909/android-startcamera-gives-me-null-intent-and-does-it-destroy-my-global-varia) – Alex Cohn Jul 05 '15 at 10:00

1 Answers1

0

fileUri is null.

Please bear in mind that your process may be terminated while you are in the background. This includes when your app is asking another app to take a picture. You need to make sure that you can rebuild your environment when your process is started up again. For example, fileUri might need to be put into the Bundle supplied to onSaveInstanceState(), then restored from the Bundle supplied to onRestoreInstanceState(). You should be doing this to deal with configuration changes as well, such as the user rotating the screen.

You can read more about the process model and handling configuration changes in the documentation.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491