Reading file gives "generic" error

I am working towards being able to embed an image into an html email message. What i need to do is read a dynamically created image in and then base64 encode it so that i can use the img embed feature of html. In the following i am simply reading in an image from the resource directory and getting a “Generic” error.

Here is the code:

 local path = system.pathForFile( "imgs/imageTitleEventRound.png", system.ResourceDirectory )  
 local fh, errStr = io.open( path, "r" )  
 print("File opened")  
 local img64 = ""  
 if fh then  
 print("reading file")  
 local contents = fh:read( "\*a" )  
 print( "Contents of " .. path .. "\n" .. contents )  
  
 img64 = base64.enc(contents)  
 else  
 print("Unable to open file: " .. errStr)  
 end  

Here is what the log says … it is failing on the fh:read command. The error is not so very helpful for me but perhaps someone behind the curtain can figure it.

2012-11-17 09:51:30.613 Corona Simulator[33057:707] File opened  
2012-11-17 09:51:30.614 Corona Simulator[33057:707] reading file  
2012-11-17 09:51:30.616 Corona Simulator[33057:707] Generic error:   
2012-11-17 09:51:30.616 Corona Simulator[33057:707] NSInvalidArgumentException: \*\*\* -[\_\_NSArrayM insertObject:atIndex:]: object cannot be nil  
(  
 0 CoreFoundation 0x00007fff8aa740a6 \_\_exceptionPreprocess + 198  
 1 libobjc.A.dylib 0x00007fff8760d3f0 objc\_exception\_throw + 43  
 2 CoreFoundation 0x00007fff8aa2481a -[\_\_NSArrayM insertObject:atIndex:] + 282  
 3 Corona Simulator 0x000000010008cfbc \_ZN6Corona10LuaLibrary10GetLibraryEP9lua\_State + 286676  
 4 Corona Simulator 0x000000010009aded luaD\_precall + 1096  
 5 Corona Simulator 0x00000001000a9fa2 luaV\_execute + 4732  
 6 Corona Simulator 0x000000010009afc6 luaD\_call + 106  
 7 Corona Simulator 0x00000001000aa66d luai\_objcttry + 25  
 8 Corona Simulator 0x000000010009b27e luaD\_pcall + 113  
 9 Corona Simulator 0x000000010009420f lua\_pcall + 102  
 10 Corona Simulator 0x0000000100057e77 \_ZN6Corona10LuaLibrary10GetLibraryEP9lua\_State + 69263  
 11 Corona Simulator 0x000000010004ef5e \_ZN6Corona10LuaLibrary10GetLibraryEP9lua\_State + 32630  
 12 Corona Simulator 0x0000000100052bde \_ZN6Corona10LuaLibrary10GetLibraryEP9lua\_State + 48118  
 13 Corona Simulator 0x0000000100053154 \_ZN6Corona10LuaLibrary10GetLibraryEP9lua\_State + 49516  
 14 Corona Simulator 0x0000000100052d35 \_ZN6Corona10LuaLibrary10GetLibraryEP9lua\_State + 48461  
 15 Corona Simulator 0x0000000100071ada \_ZN6Corona10LuaLibrary10GetLibraryEP9lua\_State + 174834  
 16 Corona Simulator 0x000000010000f4a4 start + 55956  
 17 AppKit 0x00007fff8ccee6d6 -[NSWindow sendEvent:] + 7053  
 18 AppKit 0x00007fff8ccea744 -[NSApplication sendEvent:] + 5761  
 19 AppKit 0x00007fff8cc002fa -[NSApplication run] + 636  
 20 AppKit 0x00007fff8cba4cb6 NSApplicationMain + 869  
 21 Corona Simulator 0x0000000100001a44 start + 52  
 22 ??? 0x0000000000000005 0x0 + 5  
)  
2012-11-17 09:51:30.617 Corona Simulator[33057:707] Lua Runtime Error: lua\_pcall failed with status: 2, error message is: ?:-1: attempt to call a table value  
  

[import]uid: 118012 topic_id: 33085 reply_id: 333085[/import]

Try:

local fh, errStr = io.open( path, “rb” )

I think that tells it your’re reading a binary file instead of a text file. [import]uid: 19626 topic_id: 33085 reply_id: 131378[/import]

That helped my cause thanks.

The error was actually coming from the

print( "Contents of " … path … “\n” … contents )

Guess it does not like appending binary

Cheers

[import]uid: 118012 topic_id: 33085 reply_id: 131412[/import]

That helped my cause thanks.

The error was actually coming from the

print( "Contents of " … path … “\n” … contents )

Guess it does not like appending binary

Cheers

[import]uid: 118012 topic_id: 33085 reply_id: 131413[/import]

Try:

local fh, errStr = io.open( path, “rb” )

I think that tells it your’re reading a binary file instead of a text file. [import]uid: 19626 topic_id: 33085 reply_id: 131378[/import]

That helped my cause thanks.

The error was actually coming from the

print( "Contents of " … path … “\n” … contents )

Guess it does not like appending binary

Cheers

[import]uid: 118012 topic_id: 33085 reply_id: 131412[/import]

That helped my cause thanks.

The error was actually coming from the

print( "Contents of " … path … “\n” … contents )

Guess it does not like appending binary

Cheers

[import]uid: 118012 topic_id: 33085 reply_id: 131413[/import]