I am working on a simple app to take a picture and place it in an ImageView, it works if I accept the picture, however if I choose not to use the picture, I get "java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference". Debugging it I can see mExtra=null, there I implemented a if\else statement but to no avail the app still crash if I reject the picture.
Here is the code snippet:
public class MainActivity extends AppCompatActivity {
int scoreTeamA = 0;
int scoreTeamB = 0;
Button btnTakePhoto;
ImageView imgTakenPhoto;
private static final int CAM_REQUEST = 1313;
// private static final String EXTRAS_CAMERA_FACING = "android.intent.extras.CAMERA_FACING";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnTakePhoto = (Button) findViewById(R.id.take_left_picture);
imgTakenPhoto = (ImageView) findViewById(R.id.left_player_image);//left_player_image imageView name
btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());
}
public void imgClick(View view) {
Toast.makeText(this, "imageview clicked", Toast.LENGTH_SHORT).show();
Intent cameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraintent, CAM_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAM_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
if (thumbnail == null) {
//if picture not accepted to be used set the imageView to the star2.png file
imgTakenPhoto.setImageResource(R.drawable.star2);
Toast.makeText(this, "no image to include", Toast.LENGTH_SHORT).show();
} else {
//if picture accepted use it to populate the imageView
imgTakenPhoto.setImageBitmap(thumbnail);
}
}
}
class btnTakePhotoClicker implements Button.OnClickListener {
@Override
public void onClick(View v) {
Intent cameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraintent, CAM_REQUEST);
}
}
My issue seems to be in the onActivityResult method in the if/else statement, but I am not seeing it? I would greatly appreciate your help, Thanks, Olivier