Xamarin.Mobile: problems with gallery picker (our top crasher)

nickNaylornickNaylor DEMember ✭✭
edited September 2014 in Xamarin.Android

just found what caused the top crasher in our app, but I still have no idea what I can do about it :(

I am using the MediaPicker from the Xamarin.Mobile API to let the user pick an image from the gallery.

The problem occurs when the user presses BACK or CANCEL instead of chosing an image. Most devices reports this by returning Result.Canceled, so we can test for it.

Sadly some devices (like the Nexus7 with default "Photos" app) report Result.Ok, so our current check fails and we continue, which causes the access to the result of GetMediaFileExtraAsync to hang and a second try to crash the app!

So my question is, if there is a different way to check, if the user pressed CANCEL/BACK button? I am really running out of ideas :(

My current code:

// start of picker
public static void GetImage (Fragment fragment)
    var picker = new MediaPicker (Application.Context);
    Intent intent = picker.GetPickPhotoUI ();
    fragment.StartActivityForResult (intent, AndroidConstants.REQUESTCODE_GET_GALLERY_IMAGE);

// onActivityResult gets called when image picker finishes
public override void OnActivityResult (int requestCode, Result resultCode, Intent data)
    // this line tells me, that the nexus7 returns Result.Ok
    Logger.Debug (TAG, "OnActivityResult: requestCode = " + requestCode + ", result = " + resultCode);

    if ((requestCode != AndroidConstants.REQUESTCODE_GET_GALLERY_IMAGE))

    // User canceled, this check fails on Nexus7 with default photos app
    if (resultCode == Result.Canceled)

    data.GetMediaFileExtraAsync (Application.Context)
        .ContinueWith (task => 
            // the access to the task.Result hangs the app in the first run and causes crashes with gallery picker later
            ReceiveImage (task.Result.Path);
        }, TaskScheduler.FromCurrentSynchronizationContext());

Has anyone any idea? Thanks!

Sign In or Register to comment.