I've created an activity that allows the user to add a picture to an ImageView in that activity. I've created a button, that opens a context menu for the activity and gives the user options to either pick a picture from the gallery or take a picture with camera.
If I choose the first option - to pick a picture from the gallery, it works without problems. Gallery is opened, I pick the picture, my activity is resumed and the picture is added to the ImageView.
Strange things start happening after choosing the second option, taking the picture and resuming to my activity:
- If I open the context menu again and try to open the gallery, camera activity gets opened instead
- I close the camera activity and resume to my activity, "Complete action using" dialog is shown
- I open the gallery, pick a picture and NullPointerException is thrown
Why am I having this behavior and an exception? I've tried searching for similar topics but haven't found a solution.
Below are the methods for my activity
public boolean onContextItemSelected(MenuItem item) {
super.onContextItemSelected(item);
switch(item.getItemId()) {
case R.id.cm_Select_picture: {
// TODO open gallery
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), RC_SELECT_PICTURE);
}
case R.id.cm_Take_picture: {
// TODO open camera
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, RC_TAKE_PICTURE);
}
default: return false;
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
switch(requestCode) {
case RC_SELECT_PICTURE: {
Log.d(TAG, "Case select picture");
Uri selectedImageUri = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImageUri
, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap pic = BitmapFactory.decodeFile(filePath);
goodsImage.setImageBitmap(pic);
}
case RC_TAKE_PICTURE: {
Log.d(TAG, "Case take picture");
if(data.getExtras().get("data") != null) {
Bitmap pic = (Bitmap) data.getExtras().get("data");
goodsImage.setImageBitmap(pic);
}
}
}
}
}
04-26 01:34:59.529: E/AndroidRuntime(20531): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3, result=-1, data=Intent { dat=content://media/external/images/media/9 }} to activity {com.forestassistant/com.statistics.GoodsActivity}: java.lang.NullPointerException