1

I am using below code for capture image and covert bitmap to base64 string. I am facing problem that below code sucessfully running in lollipop but when run in marshmallow its giving uri null pointer exeption. How can i solve that ?

public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == getActivity().RESULT_OK) {

            //user is returning from capturing an image using the camera
            if (requestCode == CAMERA_CAPTURE) {
                //filePath = data.getData();

                Bundle extras = data.getExtras();
                //get the cropped bitmap
                thePic = (Bitmap) extras.get("data");

                try {
                    byte[] inputData = null;
                    // String f = compressImage(uri.toString());
                    thePic = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), data.getData());


                    ByteArrayOutputStream stream = new ByteArrayOutputStream();

                    thePic.compress(Bitmap.CompressFormat.JPEG, 15, stream);

                    byte[] image = stream.toByteArray();

                    encodedString = Base64.encodeToString(image, Base64.DEFAULT);
                    //   imageView.setImageBitmap(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }


                //thePic.compress(Bitmap.CompressFormat.JPEG, 100, iStream);
                if (setInImageView.equals("1")) {

                    imageView1.setImageBitmap(thePic);
                    image1 =encodedString;

                    //Creating a shared preference
                    SharedPreferences sharedPreferences2 = getActivity().getSharedPreferences
                            (ConfigFormData.SHARED_PREF_NAME, Context.MODE_PRIVATE);
                    //Creating editor to store values to shared preferences
                    editor2 = sharedPreferences2.edit();
                    editor2.putString(ConfigFormData.METERIMAGE1_SHARED_PREF, image1);
                    //Saving values to editor
                    editor2.commit();

                } else if (setInImageView.equals("2")) {

                    imageView2.setImageBitmap(thePic);
                    image2 = encodedString;

                    //Creating a shared preference
                    SharedPreferences sharedPreferences2 = getActivity().getSharedPreferences
                            (ConfigFormData.SHARED_PREF_NAME, Context.MODE_PRIVATE);
                    //Creating editor to store values to shared preferences
                    editor2 = sharedPreferences2.edit();
                    editor2.putString(ConfigFormData.METERIMAGE2_SHARED_PREF, image2);
                    //Saving values to editor
                    editor2.commit();

                } else if (setInImageView.equals("3")) {

                    imageView3.setImageBitmap(thePic);
                    image3 = encodedString;

                    //Creating a shared preference
                    SharedPreferences sharedPreferences2 = getActivity().getSharedPreferences
                            (ConfigFormData.SHARED_PREF_NAME, Context.MODE_PRIVATE);
                    //Creating editor to store values to shared preferences
                    editor2 = sharedPreferences2.edit();
                    editor2.putString(ConfigFormData.METERIMAGE3_SHARED_PREF, image3);
                    //Saving values to editor
                    editor2.commit();

                } else if (setInImageView.equals("4")) {

                    imageView4.setImageBitmap(thePic);
                    image4 = encodedString;

                    //Creating a shared preference
                    SharedPreferences sharedPreferences2 = getActivity().getSharedPreferences
                            (ConfigFormData.SHARED_PREF_NAME, Context.MODE_PRIVATE);
                    //Creating editor to store values to shared preferences
                    editor2 = sharedPreferences2.edit();
                    editor2.putString(ConfigFormData.METERIMAGE4_SHARED_PREF, image4);
                    //Saving values to editor
                    editor2.commit();

                } else if (setInImageView.equals("5")) {

                    imageView5.setImageBitmap(thePic);
                    image5 = encodedString;

                    //Creating a shared preference
                    SharedPreferences sharedPreferences2 = getActivity().getSharedPreferences
                            (ConfigFormData.SHARED_PREF_NAME, Context.MODE_PRIVATE);
                    //Creating editor to store values to shared preferences
                    editor2 = sharedPreferences2.edit();
                    editor2.putString(ConfigFormData.METERIMAGE5_SHARED_PREF, image5);
                    //Saving values to editor
                    editor2.commit();

                }

            }
        }
    }

public void selectImage1() {
        try {
            //use standard intent to capture an image
            Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //we will handle the returned data in onActivityResult
            startActivityForResult(captureIntent, CAMERA_CAPTURE);
        } catch (ActivityNotFoundException anfe) {
            //display an error message
            String errorMessage = "oops! your device doesn't support capturing images!";
            Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT).show();
        }
    }

Below is my error

Caused by: java.lang.NullPointerException: uri
Noreen Khan
  • 233
  • 3
  • 14
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – byxor Sep 10 '16 at 16:55
  • Perhaps update your question with which line is throwing the error? – rmcsharry Sep 10 '16 at 17:08
  • Hello rmcsharry. thePic = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), data.getData()); this line throwing error. – Noreen Khan Sep 12 '16 at 05:32

0 Answers0