Search code examples
androidandroid-intentnullpointerexceptioncameraandroid-camera-intent

Null pointer exception in camera intent when i choose any third party camera Application


My problem is that every time i choose third party camera application for example Beauty Plus Camera i got null pointer exception every time, my code is completely working for default camera application it even works with Google's new camera made for moto series phones.

Very first time dialog to choose option for gallery or camera is here:

private void OpenDialogForImage() {

    final CharSequence[] items = {
            "Gallary", "Camera", "Cancel"
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            // Do something with the selection
            switch (item) {
                case 0:
                    Intent intent1 = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent1.setType("image/*");
                    startActivityForResult(
                            Intent.createChooser(intent1, "Select File"),
                            SELECT_FILE);
                    break;
                case 1:

                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(intent, REQUEST_CAMERA);

                    break;
                case 2:

                    break;
            }
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}

This is OnActivityResult() method:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == Sponser_key && resultCode == Activity.RESULT_OK) {
        String sSponsors = data.getStringExtra("Sponsors");
        if (sSponsors != null)
            sponsorsResp = new Gson().fromJson(sSponsors, GetSponsorsResp.class);
    } else if (requestCode == REQUEST_CAMERA) {


        if (resultCode == activity.RESULT_OK) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");

            ivProfile.setImageBitmap(photo);


            // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
            Uri tempUri = Other.getImageUri(activity, photo);


            file = new File(Other.getRealPathFromURI(activity, tempUri));
        } else {
            /**
             * not select any image
             */
        }
    } else if (requestCode == SELECT_FILE) {

        if (resultCode == activity.RESULT_OK) {
            Uri selectedImageUri = data.getData();
            ivProfile.setImageURI(selectedImageUri);
            file = new File(Other.getPath(activity, selectedImageUri));

        }

    }
}

This above code is note working for some third party applications like i have mentioned before. I am getting NullPointerException in this line: Bitmap photo = (Bitmap) data.getExtras().get("data");


Solution

  • If you want to leverage third-party apps, there will not be a perfect solution, because they are not required to follow the same contract as the standard Android camera app. But you can probably get better results than you are now.

    The standard camera app fulfills this contract:

    1. It returns a thumbnail in intent.getExtras.get("data")
    2. If you provide a Uri with intent.putExtra(MediaStore.EXTRA_OUTPUT, uri), it will save the full-sized image in this location.

    (See the training article for important details.)

    Although the particular third-party camera app you tried does not fulfill part 1 of the contract, you might have better luck with part 2.

    And given the full-size image, creating the thumbnail that you apparently need is not too hard, for example using answers here on Stack Overflow.