Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by linkthewise · Aug 09, 2013 at 08:38 AM · androidpluginjava

Unity 3D Java plugin issues

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IamAlan · Aug 12, 2013 at 04:51 PM 0
Share

This method should work. What error are you getting?

avatar image linkthewise · Aug 12, 2013 at 08:52 PM 0
Share

Well I got

Blockquote

JNI: Unable to find method id
avatar image linkthewise · Aug 13, 2013 at 02:13 PM 0
Share

Hi I got it eorking, it opens the gallery but i cannot return the path. Can someone please tell me what is wrong with this code?

     public static void showGallery() {
 
         UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
 
             public void run() {
 
                 intent = new Intent(
 
                         Intent.ACTION_PIC$$anonymous$$,
              android.provider.$$anonymous$$ediaStore.Images.$$anonymous$$edia.EXTERNAL_CONTENT_URI);
 UnityPlayer.currentActivity.startActivityForResult(intent, RESULT_LOAD_I$$anonymous$$AGE);
 
 
             }
 
         });
 
     }
 
      @Override
 
         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 
             super.onActivityResult(requestCode, resultCode, data);
 
             if (requestCode == RESULT_LOAD_I$$anonymous$$AGE && resultCode == RESULT_O$$anonymous$$ && null != data) {
 
                 Uri selectedImage = data.getData();
 
                 String[] filePathColumn = { $$anonymous$$ediaStore.Images.$$anonymous$$edia.DATA };
 
                 Cursor cursor = getContentResolver().query(selectedImage,
 
                         filePathColumn, null, null, null);
 
                 cursor.moveToFirst();
 
                 int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
 
                 String picturePath = cursor.getString(columnIndex);
 
                 cursor.close();
 
                 UnityPlayer.UnitySend$$anonymous$$essage("$$anonymous$$ain Camera", "Path", picturePath);
 
             }
 
         }

Thanks in advance

5 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SolidSnake · Aug 09, 2013 at 08:55 AM

I don't think it will work this way

If you want to bridge calls to android java you still need a JNI (.so) as well as the Jar. C++ bridge to be able to do calls from Unity to Java you can check the samples from the documentation:

http://docs.unity3d.com/Documentation/Manual/PluginsForAndroid.html

Check out the Java Plugin Example at the bottom of the page

Hope that will help

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by linkthewise · Aug 09, 2013 at 11:29 AM

I couldnt run it i have this

DllNotFoundException: javabridge CallJavaCode.OnGUI () (at Assets/CallJavaCode.cs:16)

Does anyone have the same problem?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Raph3004 · Aug 13, 2013 at 02:33 PM

I've always gotten my android plugins / libs to work as follows:

  • 1) write java libs in Eclipse

  • 2) include classes.jar (from Unity) in the eclipse project (project->properties->java build path->add external jar)

  • 3) write C to store pointers to java methods and act as a bridge

  • 4) place C file in eclipse project jni folder (create it if it doesn't exist)

  • 5) create a make file (.mk)

  • 6) compile the C file via cygwin (this will create the .so)

  • 7) refresh the project in eclipse

  • 8) compile eclipse project to .jar

  • 9) copy jar and .so to Unity->plugins->android

  • 10) access the functions by name (declared in the C file compiled to the .so) in unity by

    [DllImport("YOURLIBNAME")] public static extern void YOURFUNCTIONNAME(FUNCTION PARAMS);

This may or may not be the best way but it has always worked for me.

.- Raph -

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by zeusrami · Sep 03, 2013 at 11:31 PM

DId you find a solution? I have the same Problem, the onActivityResult never get called

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by _Yash_ · Apr 08, 2015 at 08:52 PM

Yessss !!! I found a solution... It is an old question but it might help the others.

first, The problem:

UnityPlayer.currentActivity is The activity which starts the activity and hence is The one which will receive onActivityResult(xx).

So, I tried extending UnityPlayerActivity, UnityPlayerNativeActivity and UnityPlayerProxyActivity and defining onActivityResult in it but it didn't worked.

Conclution: UnityPlayer.currentActivity is not any of these three.

Solution:

this is not just a solution its a Super solution because you will not need to extend unity's Activity anymore ;)

1) create a Activity which works as a router b/w UnityPlayer.currentActivity and Your target Activity ( gallery for example ).

2) Define it in your AndroidManifest file with action like this:

 <activity android:name="com.yash.androidnativeactions.router.Router">
   <intent-filter>
     <action android:name="androidnativeactions.Router" />
     <category android:name="android.intent.category.DEFAULT" />
   </intent-filter>
 </activity>

3) instead of opening gallery directly you will start router activity and router will start Gallery activity.

 Intent intent = new Intent();
 intent.setAction("androidnativeactions.Router");
 UnityPlayer.currentActivity.startActivityForResult(intent, Router.GalleryRequest);

4) inside Router.java -> onCreate() you will start gallery activity

 Intent intent = new Intent();
 
 intent.setType("image/*");
 intent.setAction(Intent.ACTION_GET_CONTENT);
 startActivityForResult(intent, GalleryRequest);

5) Now, because you have started gallery activity from Router activity you will receive onActivityResult() on Router activity instead of UnityPlayer.currentActivity. Go ahead and define onActivityResult in Router.java

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         // TODO Auto-generated method stub
         super.onActivityResult(requestCode, resultCode, data);
         if(resultCode == Activity.RESULT_OK){
             if(requestCode == GalleryRequest){
                 // fetch image uri or do whatever you like here

                 // send it to Unity ;)
                 UnityPlayer.UnitySendMessage("xx", "xx", "xx");
                 
             }
         }else{

             System.out.print("Result code in router:"+resultCode);
         }
         
         setResult(resultCode);
         finish();
     }

6) Note: do not forget to finish this Router Activity otherwise you will get stuck in b/w without any User Interface :P

I am working on Android plugin with lots of features(free) and will post the code when I'm done.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to use Android Api in Unity 1 Answer

No OpenGL Context in Unity above 2017.1.2f1 0 Answers

Using AndroidJavaClass in a java Unity3d plugin 0 Answers

Using Java Plugin to add an Android Edittext view 0 Answers

How to talk to Java plugin 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges