Open Gallery Android

hi, I use this code

function OnGUI(){
	if(GUI.Button(Rect(0,0,100,30),"Resim")){
		var path = EditorUtility.OpenFilePanel(
					"Overwrite with jpg",
					"",
					"jpg");
		var resim = WWW("file:///" + path);
		renderer.material.mainTexture=resim.texture;
	}
}

in windows. But I want to open gallery in android phone. This code not play in android. What should I do for open gallery in android and select a jpg and example: a plane renderer.material.mainTexture= my select texture. Can you help me, plz.

Hi there,

I know I’m a bit late to the party on this but I came up with my own solution and thought I would post it in case you are still trying to do this or for anyone else who finds this page.

I have written a function which gets the file paths of all the images in the android gallery. Once you have them you can load them as you would any other image on disk. My function looks like this:

    private List<string> GetAllGalleryImagePaths()
    {
        List<string> results = new List<string>();
        HashSet<string> allowedExtesions = new HashSet<string>() { ".png", ".jpg",  ".jpeg"  };

        try
        {
            AndroidJavaClass mediaClass = new AndroidJavaClass("android.provider.MediaStore$Images$Media");

            // Set the tags for the data we want about each image.  This should really be done by calling; 
            //string dataTag = mediaClass.GetStatic<string>("DATA");
            // but I couldn't get that to work...
            
            const string dataTag = "_data";

            string[] projection = new string[] { dataTag };
            AndroidJavaClass player = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject currentActivity = player.GetStatic<AndroidJavaObject>("currentActivity");

            string[] urisToSearch = new string[] { "EXTERNAL_CONTENT_URI", "INTERNAL_CONTENT_URI" };
            foreach (string uriToSearch in urisToSearch)
            {
                AndroidJavaObject externalUri = mediaClass.GetStatic<AndroidJavaObject>(uriToSearch);
                AndroidJavaObject finder = currentActivity.Call<AndroidJavaObject>("managedQuery", externalUri, projection, null, null, null);
                bool foundOne = finder.Call<bool>("moveToFirst");
                while (foundOne)
                {
                    int dataIndex = finder.Call<int>("getColumnIndex", dataTag);
                    string data = finder.Call<string>("getString", dataIndex);
                    if (allowedExtesions.Contains(Path.GetExtension(data).ToLower()))
                    {
                        string path = @"file:///" + data;
                        results.Add(path);
                    }

                    foundOne = finder.Call<bool>("moveToNext");
                }
            }
        }
        catch (System.Exception e)
        {
            // do something with error...
        }

        return results;
    }

And you can use it like this:

    [SerializeField]
    private RawImage m_image;

    public void SetImage()
    {
        List<string> galleryImages = GetAllGalleryImagePaths();
        Texture2D t = new Texture2D(2, 2);
        (new WWW(galleryImages[0])).LoadImageIntoTexture(t);
        m_image.texture = t;
    }

Hope that helps!

With some help from DeveshPandey’s comment snippet, looking around, and Share an image calling external apps on Unity for Andorid I created this method that works right inside a unity script file:

/// <summary>
/// based off 2 lines of Java code found at at http://stackoverflow.com/questions/18416122/open-gallery-app-in-androi
///      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/internal/images/media"));
///      startActivity(intent); 
/// expanded the 1st line to these 3:
///      Intent intent = new Intent();
///      intent.setAction(Intent.ACTION_VIEW);
///      intent.setData(Uri.parse("content://media/internal/images/media"));
/// </summary>
public void OpenAndroidGallery()
{
    #region [ Intent intent = new Intent(); ]
    //instantiate the class Intent
    AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");

    //instantiate the object Intent
    AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
    #endregion [ Intent intent = new Intent(); ]

    #region [ intent.setAction(Intent.ACTION_VIEW); ]
    //call setAction setting ACTION_SEND as parameter
    intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_VIEW"));
    #endregion [ intent.setAction(Intent.ACTION_VIEW); ]

    #region [ intent.setData(Uri.parse("content://media/internal/images/media")); ]
    //instantiate the class Uri
    AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");

    //instantiate the object Uri with the parse of the url's file
    AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse", "content://media/internal/images/media");

    //call putExtra with the uri object of the file
    intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject);
    #endregion [ intent.setData(Uri.parse("content://media/internal/images/media")); ]

    //set the type of file
    intentObject.Call<AndroidJavaObject>("setType", "image/jpeg");

    #region [ startActivity(intent); ]
    //instantiate the class UnityPlayer
    AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");

    //instantiate the object currentActivity
    AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");

    //call the activity with our Intent
    currentActivity.Call("startActivity", intentObject);
    #endregion [ startActivity(intent); ]
}

I don’t know what the “content://media/internal/images/media” uri is for. Changing the uri to whatever seems to do the same thing. Understanding that uri would be nice, but I was unable to find information on it.

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