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