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

TapanHP
  • 5,969
  • 6
  • 37
  • 66
  • 1
    will surely help you...http://stackoverflow.com/a/21799005/6097062 – Saurabh Vardani Jun 24 '16 at 09:54
  • 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 . – TapanHP Jun 24 '16 at 10:08
  • Avoid relying upon a third-party app...May be problem is there... and you can not handle this by urself.... – Saurabh Vardani Jun 24 '16 at 10:25
  • Ok got you !! So i must force user to use default camera application that's the only way. @Saurabh – TapanHP Jun 24 '16 at 10:27
  • 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 explanation – Saurabh Vardani Jun 24 '16 at 10:29
  • 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. @Saurabh – TapanHP Jun 24 '16 at 10:47
  • 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. – Lev Sep 02 '16 at 07:31
  • i use xolo q510s and kitkat version @Lev – TapanHP Sep 02 '16 at 08:57
  • 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. :) – Lev Sep 02 '16 at 09:27
  • 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? @Lev – TapanHP Sep 02 '16 at 10:44
  • Tampar, unfortunatelly I haven't been able to find an answer to this. – Lev Sep 02 '16 at 10:46
  • me too, i couldn't find a way @Lev – TapanHP Sep 02 '16 at 12:12

3 Answers3

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.

x-code
  • 2,940
  • 1
  • 18
  • 19
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.

SANAT
  • 8,489
  • 55
  • 66
  • this is where i get null pointer because data always returns null, when using third party camera @SANAT – TapanHP Sep 01 '16 at 14:02
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.
happydude
  • 3,869
  • 2
  • 23
  • 41
  • Can u give me example of implementing image capture myself? – TapanHP Sep 05 '16 at 01:18
  • 1
    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: https://developer.android.com/training/camera/cameradirect.html – happydude Sep 05 '16 at 01:54