I’m trying to allow the user to use an image from their photo gallery in my app. The code below works just fine and copies the image to my tmp directory.
My issue is if I try to load in the image using the following
local image = display.newImage(system.pathForFile("cardImage.jpg", system.TemporaryDirectory))
It tells me the image doesn’t exist. I print out the path and the image does indeed exist where the path is pointing. Am I able to load in an image from the tmp dir?
So the printout from the following confirms that the file exists, but corona cannot load the image?
[lua]
local path = system.pathForFile(“cardImage.jpg”, system.TemporaryDirectory)
– Determine if file exists
local fh = io.open( path )
if fh then
print( “File exists” )
local image = display.newImage(“cardImage.jpg”, system.TemporaryDirectory)
if( image ~= nil ) then
print(" – image load success.")
else
print(" – image load failed.")
end
fh:close()
else
print( “File does not exist!” )
end
[/lua]
If this returns that the file exists, but cannot load then perhaps something else is afoot, such as android “write external storage” permissions, or other factor. What OS/device are you having the problem with?
Again, you cannot load the image from the tmp directory using display.newImage(path) without having the second argument, system.TemporaryDirectory. If you do not pass a second argument, the SDK defaults to internally construct a path to the apps resource folder.
From the display.newImage docs about the baseDir argument: “Path to load the image from. Default is system.ResourceDirectory (project folder; same location as main.lua). See system.pathForFile() for valid values).”
You’re a life saver or at least an image saver. I tried the code you posted above but I forgot that I cleared my sandbox so I thought it wasn’t working. After loading the image back into the tmp folder and using the code you posted it is not working perfectly. Thanks for your help./
So the printout from the following confirms that the file exists, but corona cannot load the image?
[lua]
local path = system.pathForFile(“cardImage.jpg”, system.TemporaryDirectory)
– Determine if file exists
local fh = io.open( path )
if fh then
print( “File exists” )
local image = display.newImage(“cardImage.jpg”, system.TemporaryDirectory)
if( image ~= nil ) then
print(" – image load success.")
else
print(" – image load failed.")
end
fh:close()
else
print( “File does not exist!” )
end
[/lua]
If this returns that the file exists, but cannot load then perhaps something else is afoot, such as android “write external storage” permissions, or other factor. What OS/device are you having the problem with?
Again, you cannot load the image from the tmp directory using display.newImage(path) without having the second argument, system.TemporaryDirectory. If you do not pass a second argument, the SDK defaults to internally construct a path to the apps resource folder.
From the display.newImage docs about the baseDir argument: “Path to load the image from. Default is system.ResourceDirectory (project folder; same location as main.lua). See system.pathForFile() for valid values).”
You’re a life saver or at least an image saver. I tried the code you posted above but I forgot that I cleared my sandbox so I thought it wasn’t working. After loading the image back into the tmp folder and using the code you posted it is not working perfectly. Thanks for your help./