OnActivityResult not called after selecting image from gallery

I am trying to allow the users of my Xamarin.Android application to pick an image from their gallery and upload it to the server. The image, once picked, should first be shown in the upload form, and then converted into base64 and sent to the remote web application.

My app uses only one activity containing a FrameLayout, which gets filled with Fragments. The fragment that should handle the module (which is dynamic and depends on a schema it receives from the server) is instantiating the selection control like this:

private View BuildImageUploader()
{
    LinearLayout lay = new LinearLayout(Context);
    lay.Orientation = Orientation.Horizontal;
    lay.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
    (lay.LayoutParameters as LinearLayout.LayoutParams).SetMargins(0, 30, 0, 0);

    ImageView img = new ImageView(Context);
    var par = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent, 0.8f);
    par.Gravity = GravityFlags.Center;
    par.TopMargin = 5;          
    img.LayoutParameters = par;

    Button btn = new Button(Context);
    btn.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent, 0.2f);
    btn.Text = "Scegli";

    btn.Click += (s, e) =>
    {
        Intent intent = new Intent();
        intent.SetType("image/*");
        intent.SetAction(Intent.ActionGetContent);
        StartActivityForResult(Intent.CreateChooser(intent, "Scegli un'immagine"), 0);
    };

    lay.AddView(img);
    lay.AddView(btn);

    return lay;
}

I put the same override of OnActivityCreated in both my fragment and MainActivity:

protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
}

Yes, it does absolutely nothing and there is currently no way to get the image back, but I plan to handle that with events. The overrides are still empty because I'm testing them by putting breakpoints in them, to check which one gets called. And guess what? None. OnActivityResult is never called neither in the activity nor in the fragment. What am I doing wrong?

Extra info - what I tried so far (none worked):

  • Changing the request code (the second parameter of StartActivityForResult) to something different - I tried 1, -1 and some random 4 digits numbers as well
  • Moving the call to StartActivityForResult inside a method in class MainActivity
  • Changing the event handler lambda into an actual method

Finally, I would like to emphasize that the image picking phase works like a charm; I can see the gallery and select an image. The problem is that nothing happens after that: OnActivityResult isn't called, and I verified this with breakpoints. Thanks to whoever is willing to help, I need this for work and I'm quite desperate now, so I guess any solution will do.

Sign In or Register to comment.