I’m attempting to perform a simple image download in an application for the Android. The app works fine in the simulator, but when installed on the device the downloaded image does not display. Here is my code:
local http = require('socket.http');
local ltn12 = require('ltn12');
function loadImage( event )
if ( event.type == 'applicationStart' ) then
-- create local version of image to be downloaded
local path = system.pathForFile ( 'new\_logo.jpg', system.TemporaryDirectory );
local myFile = io.open ( path, 'w+b' );
-- download image
local r,e = http.request{
url='http://i.ehowcdn.com/ui/images/logos/new\_logo.jpg',
sink=ltn12.sink.file(myFile)
}
if ( r ) then
-- display text with status of downloaded image
local txt = display.newText ('Image Loaded ' .. tostring(e), display.contentWidth/2 - 90, display.contentHeight/2 - 30, native.systemFontBold, 20);
txt:setTextColor ( 255, 255, 255, 255 );
-- display image
local img = display.newImage ( 'new\_logo.jpg', system.TemporaryDirectory, 164, display.contentHeight/2 + 50, true );
else
-- display error message
local errortxt = display.newText ('ERROR: ' .. tostring(e), display.contentWidth/2, display.contentHeight/2, native.systemFontBold, 20);
errortxt.x = display.contentWidth/2;
errortxt.y = display.contentHeight/2 - 30;
errortxt:setTextColor ( 255, 0, 0, 255 );
end
end
end
Runtime:addEventListener ( 'system', loadImage );
-- HERE IS THE CODE FOR THE BUILD.SETTINGS FILE:
settings =
{
androidPermissions =
{
"android.permission.INTERNET"
},
}
The textfield tells me that the image was successfully downloaded as it returns the HTTP status code of 200. Also, when I go to SETTINGS>MANAGE APPLICATION and check the app it shows the app contains 12Kb of data - which is the size of the image.
[import]uid: 6297 topic_id: 1504 reply_id: 301504[/import]