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);
}
}
Here is a nice plugin to pick image and video from gallery in Android and iOS.
http://unitydevelopers.blogspot.com/2015/05/image-video-and-contact-picker-image.html
Answer by Indice_dev · Sep 13, 2017 at 08:21 PM
This link might help you. [link text]Link[1]
[1]: http://addcomponent.com/android-native-plugin-unity/
Answer by SmileSoft4849 · Apr 23, 2018 at 06:23 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
Create background service from Android Plugin 0 Answers
Black UI when building to android 1 Answer
InvokeRepeating problem 1 Answer
Icon size for IOs and Android 0 Answers