- Home /
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.
You have to write java plugin for android to open gallery or pick image from gallery.
you have to use following code in java and call it from unity.
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_VIEW);
startActivity(intent);
for more detail you can write me on devesh.pandey19@gmail.com
Browse image,video and contact in Android
https://www.assetstore.unity3d.com/en/#!/content/28597
Open gallery or another android app from unity
http://u3d.as/content/devesh-pandey/app-launcher/8$$anonymous$$H
Answer by Guy-Corbett · Feb 09, 2016 at 10:02 AM
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!
Your solution works pretty well, although I really have no idea of what's going on in it, $$anonymous$$d to explain a bit?
need manifest permission to READ_EXTERNAL_STORAGE ???
i finde the solution...need to put this line in Android$$anonymous$$anifest.xml <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Answer by ShawnFeatherly · May 22, 2015 at 11:13 PM
With some help from DeveshPandey's comment snippet, looking around, and http://www.daniel4d.com/blog/sharing-image-unity-android/ 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.
Hi,
Thanks for valuable code. Can you please tell me how to choose a particular file. I have successfully open the gallary but not able to choose image.
Thanks in Advance.
Nice plugin is here for you
http://unitydevelopers.blogspot.in/2015/05/image-video-and-contact-picker-image.html
Hi,
It works pretty weel ! Anyone knows how to select a picture ? I mean I would like to select a picture from gallery and then set it as a texture ... I heard about plugin , is it really necessary ?
Hi Jnth, Have you found a solution to select a picture with this great code ?
Thanks
Answer by SmileSoft4849 · Apr 23, 2018 at 06:27 AM
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.
Your answer
Follow this Question
Related Questions
Save Gif to Photo Gallery from Persistent Path - iOS Android 1 Answer
Download Image to a new album (Android) 0 Answers
Save a Texture2D for android 1 Answer
Apply texture to object from device gallery 0 Answers
How to get path sd card in android ? 0 Answers