Saving , retrieving Photo to and from device

I’m trying to select a pic from devices library and save it to a custom folder. Then I want to retrieve the same pic from that custom folder. Here’s my code 

main.lua ---------------------------------------------------------------------------------------- lfs = require "lfs" docs\_path = system.pathForFile( "", system.DocumentsDirectory ) success = lfs.chdir( docs\_path ) new\_folder\_path = nil dname = "EiB\_Uploads" if ( success ) then lfs.mkdir( dname ) new\_folder\_path = lfs.currentdir() .. "/" .. dname end \_G.myGameSettings = table.load( "myGameSettings.json" ) if( not myGameSettings ) then myGameSettings = {} myGameSettings.daddy = "images/templateDaddy.png" table.save( myGameSettings,"myGameSettings.json" ) end --------------------------------------------------------------------------------------- activity.lua -------------------------------------------------------------------------------------- local function loadScene() local daddy = display.newImageRect(UF,myGameSettings.daddy,\_W/5,\_W/5) daddy.x = 10 daddy.y = 10 end loadScene() local function onPhotoCompleteD(event) myGameSettings.daddy = new\_folder\_path.."/".."Daddy.jpg" table.save( myGameSettings,"myGameSettings.json" ) loadScene() end local function uploadImgDFunc(event) if (event.phase == "ended") then if media.hasSource( media.PhotoLibrary ) then media.selectPhoto( { mediaSource = media.PhotoLibrary, listener = onPhotoCompleteD, destination = {baseDir=new\_folder\_path, filename="Daddy.jpg"} } ) else native.showAlert( "Corona", "This device does not have a phot library.", { "OK" } ) end return true end end UF\_uploadImgD:addEventListener( "touch", uploadImgDFunc )

I’m getting this error : Attempt to index local daddy a nil value

I tried manually copying that pic to the specific folder (in my windows machine). But same error.

I get the same error on device too. I’m finding it very difficult to test it on simulator as library is not available on simulator. So I’m not even sure if the selected pic is copied to the custom folder or not.

Almost tried everything but no luck. Someone please guide me on where I may be going wrong 

I did the same thing. Let user select an image, make a copy of the image (to temporary directory), then use that copy.

Try using temporary directory to store your images. Hope it works.

I tried the same code to save image into system.DocumentsDirectory and it works. However, if I just browse though photos without selecting one , I get an error : attempt to index local value (nil).

It is because my code tries to find the image in documents directory but gets nil. So is there any way to know whether the user has selected a pic or not ? So that I can use that condition in my code.

Also the code doesn’t run on Marshmallow. Do I need to take some special care because I get error that:

This device does not have a photo library. I don’t know why it doesn’t detect this source on marshmallow

You will need to check whether the photo is selected or not. After that, best to check whether the file actually exist.

I didn’t encounter any issues when running on Anroid 6.x device.

Here’s a sample code I propped up and works on my Android 6 phone. Hope it helps.

function doesFileExist( fname, path ) local results = false local filePath = system.pathForFile( fname, path ) -- filePath will be nil if file doesn't exist and the path is ResourceDirectory -- if filePath then filePath = io.open( filePath, "r" ) end if filePath then -- print( "Misc : File found -\> " .. fname ) -- Clean up our file handles filePath:close() results = true else -- print( "Misc : File does not exist -\> " .. fname ) end return results end local function selectPhotoAndSaveToTemp() local selectedImgFilename = 'your\_filename.jpg' local baseDir = system.TemporaryDirectory local returnThumbnailFilename = false local displayGroup = display.newGroup() -- A function to handle the selected photo (from camera or library) local function onCompleteSelectPhoto(event) local photo = event.target if photo then -- Photo shouldn't appear! If so, this is an error as the file is saved and no photo-object will be shown -- if onErrorFunction ~= nil then onErrorFunction() end -- native.showAlert( APP\_NAME, "An error has occured when selecting photo.", { "Close" } ) else -- native.showAlert( APP\_NAME, "Ok, checking for photo-file-exist!.", { "Close" } ) -- Check whether FILE EXIST! if doesFileExist( selectedImgFilename, baseDir ) == true then -- Save a thumbnail image if returnThumbnailFilename == true then local tmpImage = display.newImage( selectedImgFilename, baseDir) tmpImage.x = display.contentCenterX; tmpImage.y = display.contentCenterY local tmpScale = math.max(display.viewableContentWidth / tmpImage.contentWidth, display.viewableContentHeight / tmpImage.contentHeight ) tmpScale = tmpScale/2 tmpImage:scale( tmpScale, tmpScale ) local captureImageOptions = { filename= 'thumb\_' .. selectedImgFilename , baseDir=baseDir, jpegQuality = 0.5, } display.save( tmpImage, captureImageOptions ) display.remove(tmpImage); tmpImage = nil end -- if onCompleteFunction ~= nil then onCompleteFunction(event) end else -- if onErrorFunction ~= nil then onErrorFunction(event) end end -- Load image file local image = display.newImage( displayGroup, selectedImgFilename , baseDir, 0,0 ) end end -- Draw a box (for iOS) to locate the selector-window local boxWidth = 10 local boxHeight = boxWidth local button = display.newRect( display.contentCenterX, display.actualContentHeight\*(1/3), boxWidth, boxHeight ) local buttonBounds = { xMin = button.contentBounds.xMin, xMax = button.contentBounds.xMax, yMin = button.contentBounds.yMin, yMax = button.contentBounds.yMax, } -- button:removeSelf(); display.remove( button ) button = nil if media.hasSource( media.PhotoLibrary ) then -- media.capturePhoto( {listener = onCompleteSelectPhoto, destination = {baseDir=baseDir, filename="jobtact\_image.jpg", type="image"} } ) media.selectPhoto( { mediaSource = media.PhotoLibrary, listener = onCompleteSelectPhoto, origin = buttonBounds, permittedArrowDirections = { "up" }, destination = {baseDir=baseDir, filename=selectedImgFilename, type="image"} , } ) else -- onErrorFunction() native.showAlert( APP\_NAME, "No media library found on this device.", { "Close" } ) end end selectPhotoAndSaveToTemp()

Thanks for the help … Your code solved many doubts … Now just have to look why there is issue with Android 6.

Thanks a ton !!

I did the same thing. Let user select an image, make a copy of the image (to temporary directory), then use that copy.

Try using temporary directory to store your images. Hope it works.

I tried the same code to save image into system.DocumentsDirectory and it works. However, if I just browse though photos without selecting one , I get an error : attempt to index local value (nil).

It is because my code tries to find the image in documents directory but gets nil. So is there any way to know whether the user has selected a pic or not ? So that I can use that condition in my code.

Also the code doesn’t run on Marshmallow. Do I need to take some special care because I get error that:

This device does not have a photo library. I don’t know why it doesn’t detect this source on marshmallow

You will need to check whether the photo is selected or not. After that, best to check whether the file actually exist.

I didn’t encounter any issues when running on Anroid 6.x device.

Here’s a sample code I propped up and works on my Android 6 phone. Hope it helps.

function doesFileExist( fname, path ) local results = false local filePath = system.pathForFile( fname, path ) -- filePath will be nil if file doesn't exist and the path is ResourceDirectory -- if filePath then filePath = io.open( filePath, "r" ) end if filePath then -- print( "Misc : File found -\> " .. fname ) -- Clean up our file handles filePath:close() results = true else -- print( "Misc : File does not exist -\> " .. fname ) end return results end local function selectPhotoAndSaveToTemp() local selectedImgFilename = 'your\_filename.jpg' local baseDir = system.TemporaryDirectory local returnThumbnailFilename = false local displayGroup = display.newGroup() -- A function to handle the selected photo (from camera or library) local function onCompleteSelectPhoto(event) local photo = event.target if photo then -- Photo shouldn't appear! If so, this is an error as the file is saved and no photo-object will be shown -- if onErrorFunction ~= nil then onErrorFunction() end -- native.showAlert( APP\_NAME, "An error has occured when selecting photo.", { "Close" } ) else -- native.showAlert( APP\_NAME, "Ok, checking for photo-file-exist!.", { "Close" } ) -- Check whether FILE EXIST! if doesFileExist( selectedImgFilename, baseDir ) == true then -- Save a thumbnail image if returnThumbnailFilename == true then local tmpImage = display.newImage( selectedImgFilename, baseDir) tmpImage.x = display.contentCenterX; tmpImage.y = display.contentCenterY local tmpScale = math.max(display.viewableContentWidth / tmpImage.contentWidth, display.viewableContentHeight / tmpImage.contentHeight ) tmpScale = tmpScale/2 tmpImage:scale( tmpScale, tmpScale ) local captureImageOptions = { filename= 'thumb\_' .. selectedImgFilename , baseDir=baseDir, jpegQuality = 0.5, } display.save( tmpImage, captureImageOptions ) display.remove(tmpImage); tmpImage = nil end -- if onCompleteFunction ~= nil then onCompleteFunction(event) end else -- if onErrorFunction ~= nil then onErrorFunction(event) end end -- Load image file local image = display.newImage( displayGroup, selectedImgFilename , baseDir, 0,0 ) end end -- Draw a box (for iOS) to locate the selector-window local boxWidth = 10 local boxHeight = boxWidth local button = display.newRect( display.contentCenterX, display.actualContentHeight\*(1/3), boxWidth, boxHeight ) local buttonBounds = { xMin = button.contentBounds.xMin, xMax = button.contentBounds.xMax, yMin = button.contentBounds.yMin, yMax = button.contentBounds.yMax, } -- button:removeSelf(); display.remove( button ) button = nil if media.hasSource( media.PhotoLibrary ) then -- media.capturePhoto( {listener = onCompleteSelectPhoto, destination = {baseDir=baseDir, filename="jobtact\_image.jpg", type="image"} } ) media.selectPhoto( { mediaSource = media.PhotoLibrary, listener = onCompleteSelectPhoto, origin = buttonBounds, permittedArrowDirections = { "up" }, destination = {baseDir=baseDir, filename=selectedImgFilename, type="image"} , } ) else -- onErrorFunction() native.showAlert( APP\_NAME, "No media library found on this device.", { "Close" } ) end end selectPhotoAndSaveToTemp()

Thanks for the help … Your code solved many doubts … Now just have to look why there is issue with Android 6.

Thanks a ton !!