Handle cancel button while using Camera
When the cancel button on the camera is pressed it returns to its present activity however I would like it to go back to the previous screen (which is a fragment).
When I press the hard key it returns as desired. This is done using finish();
EDIT: The code below is now working :)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data !=null && getInfo !=null){
if(requestCode==0){
if(resultCode == RESULT_OK && data.getAction() != null){
Bitmap theImage = (Bitmap)data.getExtras().get("data");
if(theImage !=null && getInfo !=null && data!=null){
iv.setImageBitmap(theImage);
}
}else{
finish();
}
}
else if (requestCode == 1) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor =getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
if(picturePath !=null && getInfo !=null && data!=null){
iv.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}//end if pic
}else{
finish();//returns as desired but does not work for the 'X' in the camera only hard key
}
}
Try testing against resultCode/requestCode first, something like this:
if (requestCode == 0) {
if (resultCode == RESULT_OK && data !=null ) {
... now let's see use the picture at data
Remember that resultCode
is the actuall Activity result and you should use RESULT_OK.
Also note that requestCode
only makes sense when you have more than one call for startActivityForResult
and each call for different purposes.
Hope it helps.