Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 393
TapanHP Avatar asked Jun 24 '16 09:06

TapanHP


3 Answers

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.

like image 71
x-code Avatar answered Oct 02 '22 15:10

x-code


Look into this file : https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java

Try using this :

File mPhotoFile = FileUtils.getFile(context,data.getData());

which return File.

like image 30
SANAT Avatar answered Oct 02 '22 15:10

SANAT


This is an issue with using external apps to handle functionality for you. In theory, apps that accept Intent actions should properly handle the Intent and return the data you are asking for, but in practice there is very little you can do to enforce this behavior...For example, any app can say it handles "image capture," but if you were to pass your Intent to a poorly programmed or malicious app, there is nothing preventing that app from doing something completely different than what you intended, or nothing at all. If you choose to let your app give up control to another app to fulfill certain functionality, you take the risk that whatever app is chosen cannot fulfill that functionality.

The are really very few options to always ensure that your app will take a picture the way you want it to:

  • Create a chooser for your camera Intent and limit the results to only apps that you have tested and know work as intended. If the particular apps are not installed, disable picture taking functionality.
  • Implement image capture yourself.
like image 25
happydude Avatar answered Oct 02 '22 16:10

happydude