Loading photo from tmp directory?

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.  

media.selectPhoto( { mediaSource = media.SavedPhotosAlbum, permittedArrowDirections = { "right" }, destination = {baseDir=system.TemporaryDirectory, filename="cardImage.jpg", type="image"}, listener = onComplete })

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?

Try

[lua]

local image = display.newImage(“cardImage.jpg”, system.TemporaryDirectory)

[/lua]

Okay tired that but it gives me the same file not found error.

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?

Hey this is what returns when I do this 

local path = system.pathForFile("cardImage.jpg", system.TemporaryDirectory) print(path)

“/Users/dave/Library/Application Support/Corona Simulator/Blackjack-2FF40AC40B37192AA35405528081B650/tmp/cardImage.jpg”

This image exists on the computer in this exact directory. When I try to use it like so

local path = system.pathForFile("cardImage.jpg", system.TemporaryDirectory) local backCardImage = display.newImage(path)

I get this error

Corona Simulator[24186:507] WARNING: Failed to find image(/Users/dave/Library/Application Support/Corona Simulator/Blackjack-2FF40AC40B37192AA35405528081B650/tmp/cardImage.jpg)

Here is the path showing that the image does indeed exist

8cocnID.png

I’m seeing this issue on the simulator and both iOS and Android. Here is the android permissions I’m using in the app:

androidPermissions = { "android.permission.INTERNET", "com.android.vending.BILLING", "android.permission.ACCESS\_WIFI\_STATE", "android.permission.READ\_PHONE\_STATE", "android.permission.ACCESS\_NETWORK\_STATE", "android.permission.WRITE\_EXTERNAL\_STORAGE", },

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./

cardImage.jpg and cardimage.jpg are different file names.  One has an Upper case I and one a lower case I. 

local image = display.newImage(“cardimage.jpg”, system.TemporaryDirectory)

Should work for you.

Try

[lua]

local image = display.newImage(“cardImage.jpg”, system.TemporaryDirectory)

[/lua]

Okay tired that but it gives me the same file not found error.

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?

Hey this is what returns when I do this 

local path = system.pathForFile("cardImage.jpg", system.TemporaryDirectory) print(path)

“/Users/dave/Library/Application Support/Corona Simulator/Blackjack-2FF40AC40B37192AA35405528081B650/tmp/cardImage.jpg”

This image exists on the computer in this exact directory. When I try to use it like so

local path = system.pathForFile("cardImage.jpg", system.TemporaryDirectory) local backCardImage = display.newImage(path)

I get this error

Corona Simulator[24186:507] WARNING: Failed to find image(/Users/dave/Library/Application Support/Corona Simulator/Blackjack-2FF40AC40B37192AA35405528081B650/tmp/cardImage.jpg)

Here is the path showing that the image does indeed exist

8cocnID.png

I’m seeing this issue on the simulator and both iOS and Android. Here is the android permissions I’m using in the app:

androidPermissions = { "android.permission.INTERNET", "com.android.vending.BILLING", "android.permission.ACCESS\_WIFI\_STATE", "android.permission.READ\_PHONE\_STATE", "android.permission.ACCESS\_NETWORK\_STATE", "android.permission.WRITE\_EXTERNAL\_STORAGE", },

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./

cardImage.jpg and cardimage.jpg are different file names.  One has an Upper case I and one a lower case I. 

local image = display.newImage(“cardimage.jpg”, system.TemporaryDirectory)

Should work for you.

@dmglakewood:

I meet problem like u too. it need delay it will be ok.

@dmglakewood:

I meet problem like u too. it need delay it will be ok.