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

3

8

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

Atrioventricular answered 24/6, 2016 at 9:48 Comment(12)
will surely help you...https://mcmap.net/q/1471729/-camera-app-does-not-return-to-my-activityRoyall
ok @Saurabh i checked that answer but i am getting null pointer also when not using back button as mentioned in that answer. I just click the camera button to capture image and my app Force stop .Atrioventricular
Avoid relying upon a third-party app...May be problem is there... and you can not handle this by urself....Royall
Ok got you !! So i must force user to use default camera application that's the only way. @SaurabhAtrioventricular
As u said u r using beauty plus app for camera...may be problem there...try this with some other third party camera app...and debug your code for better explanationRoyall
Ok but problem is that when i use intent ACTION_IMAGE_CAPTURE it gives me all available camera apps installed on phone and i have to choose one from them, so i can not be sure that user will choose which application from all available. @SaurabhAtrioventricular
What device are you testing this on? I had the same problem even with the default camera app on some devices. I got NPE on 2 different General Mobile models no matter what I tried.Cariotta
i use xolo q510s and kitkat version @CariottaAtrioventricular
Have you tried any other device? Like I said, this could be a device spesific problem. There are some devices that wouldn't support Google Maps api for some reason. It could also be a bug in Android code of the device. I'm not saying these are the problems, just something to keep in mind if all else fails. :)Cariotta
Ok but this problem is happen on some devices and sometimes it works perfectly, but my question is if so then how some other apps work with it perfectly? @CariottaAtrioventricular
Tampar, unfortunatelly I haven't been able to find an answer to this.Cariotta
me too, i couldn't find a way @CariottaAtrioventricular
R
2

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.

Raybin answered 5/9, 2016 at 2:33 Comment(0)
K
1

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.

Kalakalaazar answered 1/9, 2016 at 13:33 Comment(1)
this is where i get null pointer because data always returns null, when using third party camera @KalakalaazarAtrioventricular
M
1

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.
Molybdic answered 4/9, 2016 at 17:28 Comment(2)
Can u give me example of implementing image capture myself?Atrioventricular
It may be better at this point to close out this question and start another one, but here is Android's definitive training guide to get you started: developer.android.com/training/camera/cameradirect.htmlMolybdic

© 2022 - 2024 — McMap. All rights reserved.