There are multiple problems here. As Tim mentioned, you set the file name to an empty string and then append that empty string to your
uri variable. Setting the file name later has no effect on
uri (print
uri right before your
WriteFile and you'll see it's not what you're expecting). Second, Base64 encoding is used for encoding binary data. You shouldn't be using the roByteArray functions for Base64
and ASCII for the same data. It doesn't make any sense. When you do:
mapFileName=ba1.ToAsciiString()
print"decoded string in image format========>>>>>>"mapFileName
You're seeing random garbage because you're trying to print binary data, not a string. Although, if you're seeing "?PNG" then that's a good sign since that's that's the first 4 bytes of a PNG (the ? is actually 0x89 which is a non-printable character).
At the beginning of your code you should probably just have
mapFileName = "map.png"
or whatever name you'd like to use.
As for the
remove_n part, does the Base64 string returned by the API really contain the character
\ followed by the character
n or does it contain new line characters (ASCII 10 or hex 0A)? If it contains new line characters, you don't have to remove them. If it truly contains
\ followed by
n then your code to remove them won't work. You'd need:
r = CreateObject("roRegex","\\n", "")
I'd remove that part of the code until you get the other problems fixed.
Your code is a mishmash of binary and ASCII text roByteArray functions. Once you decode the Base64 encoded string, trying to print out the result won't show you much since you're trying to print binary data. You can only display the data as an image, not print it out.
To sum up, try this:
sub load_Map(json As Object)
json=parseJSON(json)
mapFileName = "map.png"
uri = "tmp:/images/"+mapFileName
ba1 = CreateObject("roByteArray")
ba1.FromBase64String(json.MapFlightResult)
ba1.WriteFile(uri)
header = CreateObject("roByteArray")
header.ReadFile(uri, 1, 3)
print header.ToAsciiString() '<--- should print PNG
end sub
After you call
load_Map, your PNG should be stored in
tmp:/images/map.png. You should then be able to use
tmp:/images/map.png as the URI for a poster.
You're real problem is probably what Tim pointed out - you're not saving the image data to a file properly.
-JT
Roku Community Streaming Expert
Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.
I am not a Roku employee.