How can I know the network.download has completed?

I want to download image from my server randomly

But, i find error when I want to use the image as following codes

I guess it is because the download process has not finished when I wan to use the image. therefore, error will be shown…

I would like to ask, how can i modify the code so that i can use the image after the it is downloaded?

[lua]local function networkListener( event )
if ( event.isError ) then
print( “Network error - download failed” )
elseif ( event.phase == “began” ) then
print( “Progress Phase: began” )
elseif ( event.phase == “ended” ) then
print( “displaying response image file” )
myImage = display.newImage( event.response.filename, event.response.baseDirectory, 60, 40 )
myImage.alpha = 0
transition.to( myImage, { alpha = 1.0 } )
end
end

local params = {}
params.progress = true

network.download(
http://54.254.168.98/image/”…math.random(1,5)…".png",
“GET”,
networkListener,
params,
“helloCopy.png”,
system.TemporaryDirectory
)
[/lua]

[lua]local photo =display.newImageRect(“ball.png”,320,480)

        photo.x = display.contentWidth/2

        photo.y = display.contentHeight/2

        photo.height= 340

        photo.width= 230

[/lua]

Try to print the “event.response.filename” after the download to check if it is showing correctly (I think I had problems with this as well at some point).

The fix was to save the name of the image locally (ie you need to use the random bere actually calling the network), and then use the name you saved to load the image, instead of the name that was returned throught event.response.

Try to print the “event.response.filename” after the download to check if it is showing correctly (I think I had problems with this as well at some point).

The fix was to save the name of the image locally (ie you need to use the random bere actually calling the network), and then use the name you saved to load the image, instead of the name that was returned throught event.response.