Hi I wanted to open the gallery of android to pick an image and send the path to unity using plugins in java for android. However all my attempts fail. Could someone tell me what im doing wrong?
1.- Create a class in eclipse
package com.Areku.GIS;
import Areku.GIS.R;
import Areku.GIS.R.id;
import Areku.GIS.R.layout;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView img;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView)findViewById(R.id.ImageView01);
((Button) findViewById(R.id.Button01)).setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public static String test() {
return "here";
}
}
2 Then I create a unity3d C# script as following.
using UnityEngine;
using System.Collections;
using System;
public class CompassJNI : MonoBehaviour {
static string zValue;
// Use this for initialization
void Start () {
AndroidJNI.AttachCurrentThread();
}
static public void StartGal() {
using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
//using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic("currentActivity")) {
AndroidJavaClass cls_CompassActivity = new AndroidJavaClass("com.Areku.GIS.MainActivity");
//cls_CompassActivity.CallStatic("Init", obj_Activity);
zValue = cls_CompassActivity.CallStatic<String>("test");
}
Debug.Log("Compass values are " + zValue.ToString());
}
}
Then I compile the class as .jar and place it at Plugins/Android.
And I put a simple function which is test to debug the communication but i wasn’t able to succeed.
Please Help being stuck 2 weeks already