Quite new to android development so hope some1 can enlighten me. Did a fair share of googling but still stuck. I am using Unity 4 and Eclipse, trying to display the Image Gallery and retrieve the Uri of the image. (Note: I am using Prime31's android social network plugin)
At first, I did not extend UnityPlayerActivity, but I realised I am unable to get onActivityResult if I don't extend it. After extending, I am getting "Can't create handler inside thread that has not called Looper.prepare()". Tried runonuithread and did not work, but might be due to my in-experience in using it. So hope you guys can help.
The following is what I have done in Eclipse:
public class AndroidGalleryPlugin extends UnityPlayerActivity{
public static AndroidGalleryPlugin plugin_instance;
public Uri ImageURI;
static final int REQUEST_CODE = 1;
static Context _context;
public static AndroidGalleryPlugin instance ()
{
if(plugin_instance == null)
plugin_instance = new AndroidGalleryPlugin();
return plugin_instance;
}
private AndroidGalleryPlugin () {}
public static void StartOpenGallery (Context mContext)
{
Log.d("AndroidGalleryPlugin", "Start Open Gallery");
_context = mContext;
Intent intent = new Intent ();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
Activity activity = (Activity) _context;
activity.startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.d("AndroidGalleryPlugin", "onActivityResult");
}
// And to convert the image URI to the direct file system path of the image file
public String getRealPathFromURI(Uri contentUri) {
Log.d("AndroidGalleryPlugin", "Get URI");
}
}
Code In Unity:
jc = new AndroidJavaClass ("com.asd.androidgallery.AndroidGallery");
jo = jc.CallStatic <AndroidJavaObject> ("instance");
if(jo == null)
{
Debug.LogError ("Java Obj Null");
return false;
}
unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
unityObj = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
if(unityObj == null)
{
Debug.LogError ("Unity Obj Null");
return false;
}
jo.CallStatic("StartOpenGallery", unityObj);
This is my androidmanifest:
<activity android:name="com.prime31.UnityPlayerNativeActivity" android:label="@string/app_name" android:screenOrientation="sensorPortrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
</activity>
<activity android:name="com.asd.androidgallery.AndroidGalleryPlugin" android:configChanges="orientation"></activity>
Here are some changes, first for your java code
public class AndroidGalleryPlugin extends UnityPlayerActivity{
public Uri ImageURI;
static final int REQUEST_CODE = 1;
private Activity _activity;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
_activity = UnityPlayer.currentActivity;
}
public static void StartOpenGallery (Context mContext)
{
Log.d("AndroidGalleryPlugin", "Start Open Gallery");
_context = mContext;
Intent intent = new Intent ();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
_activity.startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.d("AndroidGalleryPlugin", "onActivityResult");
}
// And to convert the image URI to the direct file system path of the image file
public String getRealPathFromURI(Uri contentUri) {
Log.d("AndroidGalleryPlugin", "Get URI");
}
Added onCreate
method which I get Unity's current activity from there. Also the constructor methods are unnecessary for your class since its an activity.
For Unity since you are overriding your activity you just need to get your get your unityplayer class not the instance! Just use this code.
unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
unityObj = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
if(unityObj == null)
{
Debug.LogError ("Unity Obj Null");
return false;
}
unityObj.CallStatic("StartOpenGallery");