MemoryBlock in Android not accepting string value

I have the following code verbatim in an iOS app in the URLConnection ContentReceived but trying to get this to work in Android

Dim mb As MemoryBlock = content

Var outputStream As BinaryStream = BinaryStream.Create(folPathQuestion, True)
outputStream.Write(mb)

When trying to test this in Android, I am getting the following errors

Type mismatch error. Expected class MemoryBlock, but got String
(this is on the “Dim mb As MemoryBlock = content” line)

Parameter “value” expects type String, but this is class MemoryBlock
(this is on the “outputStream.Write(mb)” line)

Do I need to handle a MemoryBlock differently on Android? When hovering over MemoryBlock in code in the iOS project, I see the tip “MemoryBlock.Constructor(bytes as Integer)”. And hovering over in the Android project I see the same but also “MemoryBlock.Constructor(data as String)”. So not sure if that’s something worth noting

Using Xojo 2024 R3

There are currently small differences with MemoryBlock in Xojo Android compared to iOS. Do you have access to the Testers channel to test upcoming Xojo versions? Could make a difference in this case. :wink:

Here’s code tested in 2024r3.1:

Dim content As String  = "Cool"
Dim mb As MemoryBlock
Dim s As String

#If TargetAndroid And XojoVersion < 2024.04
  mb = New MemoryBlock(content)
  s = mb.StringValue(0, content.Bytes)
#Else
  mb = content
  s = mb
#EndIf

Var outputStream As BinaryStream = BinaryStream.Create(SpecialFolder.Documents.Child("test.txt"), True)
outputStream.Write(s)
outputStream.Close

Yes, I think I’m on the testers channel. I might just have to try out the beta! Good to know I’m not going crazy

Thanks so much for the sample code for r3.1! I’ll give it a go tomorrow

1 Like

Does it work if you first convert the MB to a string and pass that to the Write() method?

That’s what my code does :wink:

1 Like

@Martin_T confirming your code works with Xojo version < 2024 r4. Thank you for this!

1 Like