0

I have an app in which I can open camera and take a photo and to save gallery. When I click on button to open camera its works. If I click the back button on the phone without taking a photo, my application is stopped. How can I solve this problem ?

    Button camera = (Button) findViewById(R.id.camer);
    camera.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);


        }
    });

    send.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent i = new Intent(Intent.ACTION_SEND);


            //Log.d("URI@!@#!#!@##!", Uri.fromFile(pic).toString() + "   " + pic.exists());
            if (pic != null) {
                i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));
            }

            i.setType("image/jpg");

            startActivity(Intent.createChooser(i, "Share you on the jobing"));
        }
    });


}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST) {
        thumbnail = (Bitmap) data.getExtras().get("data");
        image = (ImageView) findViewById(R.id.imageToUpload);

        image.setImageBitmap(thumbnail);


        try {
            File root = Environment.getDataDirectory();
            if (root.canWrite()) {
                pic = new File(root, "pic.jpg");
                FileOutputStream out = new FileOutputStream(pic);
                thumbnail.compress(CompressFormat.JPEG, 100, out);
                out.flush();
                out.close();
            }

        } catch (IOException e) {
            Log.e("BROKEN", "Could not write file " + e.getMessage());
        }

    }

}

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
k.kbr
  • 45
  • 6

2 Answers2

0

Its because your data is null

Try this way :

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if(requestCode == CAMERA_PIC_REQUEST){
   if (resultCode == RESULT_OK) {
       thumbnail = (Bitmap) data.getExtras().get("data");
       image = (ImageView) findViewById(R.id.imageToUpload);

       image.setImageBitmap(thumbnail);


       try {
           File root = Environment.getDataDirectory();
             if (root.canWrite()) {
                pic = new File(root, "pic.jpg");
                FileOutputStream out = new FileOutputStream(pic);
                thumbnail.compress(CompressFormat.JPEG, 100, out);
                out.flush();
                out.close();
            }

         } catch (IOException e) {
             Log.e("BROKEN", "Could not write file " + e.getMessage());
         }

      }
   }

}

More reference : Getting a Result from an Activity

Santhosh Joseph
  • 744
  • 7
  • 18
0

Try this

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == RESULT_OK) {

       if (requestCode == CAMERA_PIC_REQUEST) {
                    Bitmap thumbnail = (Bitmap)data.getExtras().get("data");

                }

   }
        if (resultCode == RESULT_CANCELED) {
            return;
        }
        }


            }
Bunny
  • 1,044
  • 12
  • 23