Read Photo from Documents Directory

Hello everyone,

I have a button that lets a user pick a photo from the device’s photo library. After the user chooses a file, the file is stored in the system.DocumentsDirectory and the path to the file is saved in a SQL Database. The problem is that when I try to read the file from the Documents directory using the path stored in the database, the code fails, claiming there is no image at the specified location (even though when I put the path into my Mac, it finds the photo immediately).

Does anyone have any advice of experience with saving and then loading a photo that is stored in the Documents directory? Any help would be appreciated.

Thank you.

Can you show the relevant code?  Have you verified the path you are using to read the file?

I have tried to verify the path. Using system.pathForFile and io:open the file exists.

Here’s some of the code I’m using

[lua]

local librarySessionComplete = function(event)

                photo = event.target

    

                if photo then

                    if photo.width > photo.height then

                        photo:rotate( -90 )            – rotate for landscape

                        print( “Rotated” )

                        print("W: "…photo.width)

                    end

        

                    local xScale = display.contentWidth / photo.contentWidth

                    local yScale = display.contentHeight / photo.contentHeight

                    local scale = math.max( xScale, yScale ) * .75

                    photo:scale( scale, scale )

                    photo.x = display.contentCenterX

                    photo.y = display.contentCenterY

                    display.save( photo, “Photo1-”…tonumber(gatheredId)…".png", system.DocumentsDirectory )

                    local docuPath = “Photo1-”…tonumber(gatheredId)

                    local updatePhoto = “UPDATE user_entries SET photo=’”…docuPath…"’ WHERE id=’"…gatheredId…"’"

                    db:exec(updatePhoto)

end

local pulledPhoto1 = {}

            photo1IntAdded = 1

            for row in db:nrows(“SELECT id, photo FROM user_entries WHERE id=’”…gatheredId…"’") do

                pulledPhoto1[photo1IntAdded] = row.photo

                photo1IntAdded=photo1IntAdded+1

            end

local picPath = system.pathForFile( tostring(pulledPhoto1[row.index])…".png", system.DocumentsDirectory)

            local openPic = io.open(picPath, “r+”)

            if openPic then

                print("FILE FOUND AT: "…picPath)

                --row.photo =

                print(openPic:read("*a"))

            else

                print(picPath)

                print(openPic)

                print(“ERROR”)

            end

            

            photo1 = widget.newButton

            {

                left = 1180,

                top = display.contentCenterY - 150,

                width = 60,

                height = 90,

                defaultFile = system.pathForFile( tostring(pulledPhoto1[row.index])…".png", system.DocumentsDirectory),

                onEvent = choosePhoto,

            }

 [/lua]

Does that code help figure out what I’m trying to do? Is what I’m trying to do even possible in Corona?

I have figured out my issue. I was over complicating things.

Thank you.

Can you show the relevant code?  Have you verified the path you are using to read the file?

I have tried to verify the path. Using system.pathForFile and io:open the file exists.

Here’s some of the code I’m using

[lua]

local librarySessionComplete = function(event)

                photo = event.target

    

                if photo then

                    if photo.width > photo.height then

                        photo:rotate( -90 )            – rotate for landscape

                        print( “Rotated” )

                        print("W: "…photo.width)

                    end

        

                    local xScale = display.contentWidth / photo.contentWidth

                    local yScale = display.contentHeight / photo.contentHeight

                    local scale = math.max( xScale, yScale ) * .75

                    photo:scale( scale, scale )

                    photo.x = display.contentCenterX

                    photo.y = display.contentCenterY

                    display.save( photo, “Photo1-”…tonumber(gatheredId)…".png", system.DocumentsDirectory )

                    local docuPath = “Photo1-”…tonumber(gatheredId)

                    local updatePhoto = “UPDATE user_entries SET photo=’”…docuPath…"’ WHERE id=’"…gatheredId…"’"

                    db:exec(updatePhoto)

end

local pulledPhoto1 = {}

            photo1IntAdded = 1

            for row in db:nrows(“SELECT id, photo FROM user_entries WHERE id=’”…gatheredId…"’") do

                pulledPhoto1[photo1IntAdded] = row.photo

                photo1IntAdded=photo1IntAdded+1

            end

local picPath = system.pathForFile( tostring(pulledPhoto1[row.index])…".png", system.DocumentsDirectory)

            local openPic = io.open(picPath, “r+”)

            if openPic then

                print("FILE FOUND AT: "…picPath)

                --row.photo =

                print(openPic:read("*a"))

            else

                print(picPath)

                print(openPic)

                print(“ERROR”)

            end

            

            photo1 = widget.newButton

            {

                left = 1180,

                top = display.contentCenterY - 150,

                width = 60,

                height = 90,

                defaultFile = system.pathForFile( tostring(pulledPhoto1[row.index])…".png", system.DocumentsDirectory),

                onEvent = choosePhoto,

            }

 [/lua]

Does that code help figure out what I’m trying to do? Is what I’m trying to do even possible in Corona?

I have figured out my issue. I was over complicating things.

Thank you.