- Home /
Call native android constructor
I want to create instance of native android File class. For that I have used following code
AndroidJavaClass fileClass = new AndroidJavaClass("java.io.File");
AndroidJavaObject fileObject = new AndroidJavaObject("java.io.File");
But after seeing java api, I found that File class contains one argument in constructor no constructor with empty argument available. In unity, I don't know way to pass argument within constructor and I want to use FIle class instance variable.
Please give some guidance in this.
Answer by liortal · Oct 23, 2014 at 09:49 PM
The following code:
AndroidJavaObject fileObject = new AndroidJavaObject("java.io.File");
Is equivalent to calling this Java code:
File fileObject = new File();
The AndroidJavaObject constructor allows passing parameters that will be in turn passed along to the Java constructor.
So, in order to initialize a File object, pass the correct constructor arguments, e.g:
// Call the File(string path) constructor
AndroidJavaObject fileObject = new AndroidJavaObject("java.io.File", "/some/path/somePath.txt");
Thanks a lot, I learn lots of new things from link also.
Answer by kromenak · Oct 23, 2014 at 06:30 PM
According to http://docs.unity3d.com/ScriptReference/AndroidJavaObject-ctor.html, you can pass arguments when you create an AndroidJavaObject. The examples show how this would work.
The AndroidJavaObject accepts a list of arguments as it's second parameter, which should allow you to call whatever version of the File class constructor that you'd like.
Thanks for your help but other answer is more specific to answer so make that correct.
Your answer
Follow this Question
Related Questions
Multiple plugin conflict 8 Answers
Issue with intent filters in AndroidManifest.xml 0 Answers
native android plugin 0 Answers
Android - Trying to Load Library bug 0 Answers
A node in a childnode? 1 Answer