Hi mastersdktester,
I had this issue while I tried to streamline my app. Here is how I implemented a way to check whether the file exist in the tmp-folder (note that it gets deleted by Android/iOS)
function extractImageName( url ) -- This will loop and find the last occurence of the character local function searchChar(url, index, char) local startOfFilename = string.find( url, char, index) -- print(startOfFilename) if startOfFilename ~= nil then return searchChar(url, startOfFilename + 1, char) else return index end end local startOfFilename = searchChar( url, 1, "/" ) -- print(startOfFilename) local returnData if startOfFilename == nil then returnData = nil else returnData = string.sub(url, startOfFilename ) end return returnData end function doesFileExist( fname, path ) local results = false local filePath = system.pathForFile( fname, path ) if filePath then filePath = io.open( filePath, "r" ) end if filePath then -- Clean up our file handles filePath:close() results = true else print( "File does not exist -\> " .. fname ) end return results end
Here is how to use it :
local url = "http://eofdreams.com/data\_images/dreams/image/image-07.jpg" local imgFilename = extractImageName( url ) if imgFilename == nil then -- No filename found! Edit this depending on how to want to handle it. return end local function networkListener( event ) if ( event.isError ) then print ( "ERROR RESPONSE: ", event.response.filename ) else print ( "RESPONSE: ", event.response.filename ) end end if doesFileExist( imgFilename, system.TemporaryDirectory) then print("Picture file exist! " .. imgFilename) -- Use it! local imgImage = display.newImageRect(imgFilename , system.TemporaryDirectory, 100, 100) else local tmpID = network.download( url, "GET", networkListener, imgFilename, system.TemporaryDirectory ) -- save the download id, so you can cancel it if needed (e.g. user leaves the page) table.insert(g\_downloadID, tmpID) end