Android Question How to code JavaObject ?

Theera

Expert
Licensed User
Longtime User
I 've studied about JavaObject code from Alexander
B4X:
'Non-B4XPages
Public Sub  Thaiwordbreak(InputStr As String) As String()
    Dim twb As JavaObject
    twb.initializeContext
    Dim mystring As String = InputStr  'ค่าเริ่มอาจไม่ใช่ InputStr ก็ได้  เช่น อาจเท่ากับ ""
    ' Use twb = Me if you have the code in a B4XPage
    mystring = twb.runMethod("ThaiWordBreakJava", Array(mystring))
    'Log(mystring)
    Return  mystring
End Sub
B4X:
'B4XPages
Public Sub  Thaiwordbreak(InputStr As String) As String()
    Dim twb As JavaObject=Me'<--- Add  equal to Me
    ' twb.initializeContext  <---delete this line
    Dim mystring As String = InputStr 'ค่าเริ่มอาจไม่ใช่ InputStr ก็ได้  เช่น อาจเท่ากับ ""
    ' Use twb = Me if you have the code in a B4XPage
    mystring = twb.runMethod("ThaiWordBreakJava", Array(mystring))
    'Log(mystring)
    Return  mystring
End Sub

Now,I 've a problem about JavaObject again.
B4X:
    'From Erel's code
    'B4XPages
    '.....
    Dim result As Intent = Args(1)
        Dim jo As JavaObject = result
        Dim ctxt As JavaObject
        Dim out As OutputStream = ctxt.InitializeContext.RunMethodJO("getContentResolver", Null).RunMethod("openOutputStream", Array(jo.RunMethod("getData", Null)))

       
        File.Copy2(Source, out)
        out.Close
        Return True
    End If
    Return False
End Sub

I have tried code this,but it is not success. I would like someone help me resolved this problem.
B4X:
   'Non-B4XPages
    '.....
    Dim result As Intent = Args(1)
'        Dim jo As JavaObject = result
'        Dim ctxt As JavaObject
'        Dim out As OutputStream = ctxt.InitializeContext.RunMethodJO("getContentResolver", Null).RunMethod("openOutputStream", Array(jo.RunMethod("getData", Null)))

        Dim jo As JavaObject
        Dim  ctxt As String=result
        ctxt=ctxt.RunMethodJO("getContentResolver", Null).RunMethod("openOutputStream", Array(jo.RunMethod("getData", Null)))
       
        File.Copy2(Source, out)
        out.Close
        Return True
    End If
    Return False
End Sub
 

aeric

Expert
Licensed User
Longtime User
You need to cast the object to the correct type.

You wrote:
B4X:
Dim  ctxt As String
ctxt is String

B4X:
ctxt.RunMethodJO("getContentResolver", Null)
this will be invalid.

Try change to:
B4X:
Dim ctxt As JavaObject = result

Again, DON'T write B4XPages and non-B4XPages.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
If I understand your words,it's belows.
B4X:
'        Dim jo As JavaObject = result
'        Dim ctxt As JavaObject
'        Dim out As OutputStream = ctxt.InitializeContext.RunMethodJO("getContentResolver", Null).RunMethod("openOutputStream", Array(jo.RunMethod("getData", Null)))


        Dim jo As JavaObject
        jo.initializeContext
        Dim ctxt As JavaObject=result
        Dim out As OutputStream = ctxt.InitializeContext.RunMethodJO("getContentResolver", Null).RunMethod("openOutputStream", Array(jo.RunMethod("getData", Null)))
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
In my head,there are 2 javaobject overlap together,but I don't get understand how to code correctly.

B4X:
    Dim ctxt As JavaObject
        ctxt.InitializeContext
        Dim mystring As String=result
        mystring=ctxt.RunMethod("openOutputStream", Array(jo.RunMethod("getData", Null)
        
        Dim jo As JavaObject

        jo.initializeContext
        
        Dim mystring2 As String=mystring
        mystring2=jo.RunMethod("getContentResolver", Null)
        
        Dim out As OutputStream=mystring2
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Understand what is Java Object (or JavaObject)
As the name explains itself, this is something to do with Java.

Explanation:
To understand, you need to first understand what is object and class in programming.
This is a very basic concept that you first need to know in programming!

Let's take String class as an example, you can create a variable call "name"
B4X:
Dim name As String
Now name is an object of String type, which you can think it is from String class.
You can call any method (except it is Private) from String type.
example:
B4X:
name.ToLowerCase

This type of object or class has methods that you can access through B4X IDE Intellisense when you type a dot.
1752040929407.png


JavaObject library allows you to use B4X to access Java (non B4X) class, methods and properties.
1752041243526.png


You cannot treat String same as JavaObject. They are different types.

If the JavaObject calls a method to return String value and you assign it to a String object then the object is no longer a JavaObject.
example:
B4X:
Dim jo As JavaObject = Something
Dim name As String = jo.RunMethod("getString", Null)
Log(name.ToLowerCase)                   '✔️
Log(name.RunMethod("getData", Null))    '❌

To use JavaObject, you must fully understand what type of object you are working with and what methods are available. The methods are not listed in B4X IDE Intellisense.

Please read and understand the tutorials of using JavaObject.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
I've learn to split JavaObjectNested. It's ok for B4XPages,but it's not supported Non-B4XPages
B4X:
'Original code
Dim result As Intent = Args(1)
Dim jo As JavaObject = result
Dim ctxt As JavaObject
Dim out As OutputStream = ctxt.InitializeContext.RunMethodJO("getContentResolver", Null).RunMethod("openOutputStream",Array(jo.RunMethod("getData", Null)))


B4X:
 'Split Code
       '----------------------This section for B4XPages  is like post#1---------------------
'       Dim result As Intent = Args(1)
'       Dim jo As JavaObject = result
        Dim jo As JavaObject = Args(1)

        Dim fileUri As JavaObject   '<------------Refer to JavaObject  instead of String which like in post#1
        fileUri= jo.RunMethod("getData", Null)
        '-----------------------------------------------------------------------------------------
        '-----------------------This section for Non-B4XPages is like post#1---------------------------------------
        Dim ctxt As JavaObject
        ctxt.InitializeContext
        Dim contentResolver As JavaObject   '<------------Refer to JavaObject  instead of String which like in post#1
        contentResolver= ctxt.RunMethod("getContentResolver", Null)
        '------------------------------------------------------------------------------------------------------------
        Dim out As OutputStream = contentResolver.RunMethod("openOutputStream", Array(fileUri))
 
Last edited:
Upvote 0
Top