Pick image from android gallery

So this has been asked before but there was no answer except for go buy someones asset, which is unacceptable for me because I need to know how to do it.

I’m working entirely from c# using AndroidJavaClasses, AndroidJavaObjects and AndroidJavaProxies. My current code is below with my three failed solutions.

Anyone got any ideas??

using UnityEngine;
using UnityEngine.UI;

public class ImagePicker : MonoBehaviour {

    class OnActivityResultCallback : AndroidJavaProxy {
        public Text textbox;

        public OnActivityResultCallback() : base("android.app.Activity") {

        protected void onActivityResult(int requestCode, int resultCode, AndroidJavaObject intent) {
            textbox.text += "onActivityResult: " + requestCode;


    public Text textbox;

    public void RequestImageFromGallery() {

        AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
        AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");

        intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_GET_CONTENT"));
        intentObject.Call<AndroidJavaObject>("setType", "image/*");

        OnActivityResultCallback callback = new OnActivityResultCallback();
        callback.textbox = textbox;

        // Solution 1, use the Unity current activity, problem: no onActivityResult to return result to
        AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject UnityActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
        UnityActivity.Call("startActivityForResult", intentObject, 1);

        // Solution 2, use a AndroidJavaProxy with onActivityResult implemented, problem: there is no activity instance in a proxy and so gives an error about application threads being null
        callback.Invoke("startActivityForResult", new object[] { intentObject, 1 });

        // Solution 3, use java reflection from c# to call the startActivityForResult method with the callback as the "this" object, problem: I get an error when trying to get the getMethod method and I'm not sure it'll work anyway
        // class intClass = java.lang.Integer.class; (ish)
        AndroidJavaClass intClass = new AndroidJavaClass("java.lang.Integer");
        // java.lang.Class jclass = obj.getClass();
        // java.lang.reflect.Method method = jclass.getMethod(methodName, param1.class, param2.class, ..);
        AndroidJavaClass jclass = UnityActivity.Call<AndroidJavaClass>("getClass");
        AndroidJavaObject method = jclass.Call<AndroidJavaObject>("getMethod", "startActivityForResult", intentClass, intClass);
        // method.invoke(obj, arg1, arg2,...);
        method.Call("invoke", callback, intentObject, 1);



This link might help you.
[link text]Link1

I have created “Android Native Gallery Item Picker” for this task. It can pick single or multiple image and video from your android phone or android gallery.

Click here for Android Native Gallery Item Picker