Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Android class not found exception

Discussion in 'Android' started by aubergine, May 4, 2015.

  1. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,626
    Whenever i try this:
    AndroidJavaObject jo = new AndroidJavaObject("android.hardware.camera2");
    or this;
    AndroidJavaClass jc = new AndroidJavaClass("android.hardware.camera2.CameraDevice");

    I get a class not found exception error in logcat.

    How do i get direct Access to android camera and its parameters from Unity?
     
  2. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,626
    Well i decided to check one of the tiny scripts in the documents. I tried this:
    AndroidJavaClass jc = new AndroidJavaClass("android.os.Build");
    string manufacturer = jc.Get<string>("MANUFACTURER");

    and here is the logcat message;
    It seems to be there on the android documents. What might be wrong?
     
  3. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,626
    Oh the problem with the documents is that its poorly written.
    MANUFACTURER is a static variable, so it should be;
    string manufacturer = jc.GetStatic<string>("MANUFACTURER");

    But the first problem with the camera is still there and cant solve it :(
     
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,318
    The MANUFACTURER field is a static field, hence you should access it like this:
    Code (CSharp):
    1. AndroidJavaClass jc = new AndroidJavaClass("android.os.Build");
    2. string manufacturer = jc.GetStatic<string>("MANUFACTURER");
     
  5. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,318
    Regarding the camera issues, you are trying to call this code:
    Code (CSharp):
    1. AndroidJavaObject jo = new AndroidJavaObject("android.hardware.camera2");
    which is like the equivalent of calling the camera2 constructor with no arguments.

    This is a package, and not a class; that's why you're probably getting those errors.

    What would you like to achieve with the camera? see the documentation here: http://developer.android.com/reference/android/hardware/camera2/package-summary.html
     
  6. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,626
    Well my ultimate goal is to be able to alter CONTROL_AWB_MODE_TWILIGHT from unity somehow.
    How should i proceed?
     
  7. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,318
    How is it done on Android? do you have any reference code ?
     
  8. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,626
    Its in the CameraCharacteristics class, first i need to get an instance for the;
    And then get CameraCharacteristics with
     
  9. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,318
    This will only get the characteristics. Do you want to set them as well ?
     
  10. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,626
    yep
     
  11. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,318
    This code does what you want:
    Code (CSharp):
    1. using (var player = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    2. {
    3.     using (var activity = player.GetStatic<AndroidJavaObject>("currentActivity"))
    4.     {
    5.         var cameraManager = activity.Call<AndroidJavaObject>("getSystemService", "camera");
    6.  
    7.         AndroidJavaObject cameraCharacteristics = cameraManager.Call<AndroidJavaObject>("getCameraCharacteristics", cameraId);
    8.  
    9.         // Do what you need here ...
    10.     }
    11. }
     
  12. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,626
    I will try this code tonight to see how it works, thank you for the code.
    I just didnt understand the relation between currentActivity of Unity and the Context class of android.
     
  13. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,318
    On Android, Activity is derived from Context.

    Unity has a class called UnityPlayer that gets assigned the game's activity.

    When the game starts, something like this happens (really simplified code, just to illustrate the point):
    Code (CSharp):
    1. public class UnityPlayerActivity {
    2.    
    3.     @Override
    4.     protected void onCreate(Bundle savedInstanceState) {
    5.  
    6.         // keep a static reference to the game's activity
    7.         UnityPlayer.currentActivity = this;
    8.     }
    9. }
    This makes it easier for you to grab the activity from C# code (like the code i showed above).
     
  14. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,626
    I see, now its all clear. Thank you :)
     
  15. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,626
    Well, i tried the code. And the below line gives errors on logcat:
    var cameraManager = activity.Call<AndroidJavaObject>("getSystemService", "camera");

    Here is the error:
     
  16. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,318
  17. mrpmorris

    mrpmorris

    Joined:
    Dec 25, 2012
    Posts:
    47
    Thanks for this code, it was very helpful!
     
    liortal likes this.
  18. Sask34

    Sask34

    Joined:
    Aug 1, 2016
    Posts:
    2
    I have a problem using AndroidJavaClass It always Gives a JavaClassNotFoundException
    Here is my C# code:

    public class TryVoice : MonoBehaviour {
    public Text textMesh;
    // Use this for initialization
    public void GetTextFromVoice(){
    try{
    var plugin = new AndroidJavaClass ("thesis.plugin.voiceRecognition");
    textMesh.text = plugin.Call<string> ("sendSaidWord");
    }
    catch(AndroidJavaException ex){
    textMesh.text = ex.ToString ();
    }
    }
    }

    And my AndroidJavaClass:

    import android.support.v7.app.AppCompatActivity;
    import thesis.plugin.utils.ConversionCallaback;
    import android.widget.Toast;

    public class voiceRecognition extends AppCompatActivity implements ConversionCallaback {
    public static String SaidWord = "";
    private static final int STT = 1;
    private static int CURRENT_MODE = -1;

    public String sendSaidWord() {
    doSendSome();
    return SaidWord;
    }

    public void doSendSome() {
    Toast.makeText(this, "Looking For Voice" , Toast.LENGTH_SHORT).show();
    TranslatorFactory.getInstance().getTranslator(TranslatorFactory.TRANSLATOR_TYPE.SPEECH_TO_TEXT, voiceRecognition.this)
    .initialize("Hello There", voiceRecognition.this);
    CURRENT_MODE = STT;
    }

    @override
    public void onSuccess(String result) {
    switch (CURRENT_MODE) {
    case STT:
    SaidWord = result;
    }
    Toast.makeText(this, "Result " + result, Toast.LENGTH_SHORT).show();
    }

    @override
    public void onCompletion() {
    Toast.makeText(this, "Done ", Toast.LENGTH_SHORT).show();
    }

    @override
    public void onErrorOccured(String errorMessage) {
    Toast.makeText(this, "Error " + errorMessage, Toast.LENGTH_SHORT).show();
    }
    }
     
  19. iamsidv

    iamsidv

    Joined:
    Jan 24, 2013
    Posts:
    17

    Hi, were you able to figure out about this issue??
    Please let me know if you've resolved it by any chance.