- Home /
pick image from android gallery
Hello. I'm searching a way to load image from android gallery. I'm found that: http://answers.unity3d.com/questions/537476/open-gallery-android.html
but it is just open gallery. After a little work I simple modified it:
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_GET_CONTENT); ]
//call setAction setting ACTION_SEND as parameter
intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_GET_CONTENT"));
#endregion [ intent.setAction(Intent.ACTION_GET_CONTENT); ]
#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 [ startActivityForResult(intent , 1); ]
//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 [ startActivityForResult(intent , 1); ]
}
Now it open gallery and I can choose image. Look at the last region. But how I can use it as a texture for example? How catch uri of image?
I think I can use some of unity assets, but now I try to implement it without assets. And I hope it would be interesting for other beginners.
Yeh I have the same problem :/ have someone a result ??
Did anyone find a solution to this? I would really like to know. Thanks!
Answer by pafla · Sep 16, 2015 at 01:48 PM
For an ordinary Android App, the results would be returned via the onActivityResult() callback function. So your Activity needs to override that function. The best way to accomplish that is to write a Java Plugin. The PlugIn could for instance extend the UnityPlayerActivity, override onActivityResult(), obtain the uri and send it to your Unity Code. Instructions on how to do this can be found in the docs. http://docs.unity3d.com/Manual/PluginsForAndroid.html
You could also move all your image loading code to Java, which would be a lot cleaner.
Your answer
Follow this Question
Related Questions
How to load image from android gallery 0 Answers
Save a Texture2D for android 1 Answer
Image Not Sharing after Oreo version? 0 Answers
How to save an in game screenshot directly to gallery? 1 Answer
Load Image from local folder on Android not working 2 Answers