Search code examples
androidonactivityresultstartactivityforresult

onActivityResult user cancels the activity


I have a activity call for getting image

startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);

On my onActivityResult I want to handle the scenario if the user cancels the operation in between.

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

            Uri selectedImageUri = data.getData();
            String selectedImagePath = getPath(selectedImageUri);
            if (selectedImagePath != null) {
                 //do something
           }
            else
                Log.i("At the","selectedimagenull");
            }

        else
            Log.i("At the","SELECT PICTURENULL");
    }
}

But if the user cancels the operation nowhere am getting information. I've tried to debug but its not catching anywhere. What am doing wrong here ?


Solution

  • public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
    
                Uri selectedImageUri = data.getData();
                String selectedImagePath = getPath(selectedImageUri);
                if (selectedImagePath != null) {
                     //do something
               }
                else
                    Log.i("At the","selectedimagenull");
                }
    
            else
                Log.i("At the","SELECT PICTURENULL");
        }
      else
      Log.i("At the","result not ok");  // User cancelled the activity
    }