How to save a photo to phone using media api

For the life of me I cannot get the media api to save a photo to the phone under a predefined name. I am testing on Android and every time the picture gets saved in /storage/emulated/0/Pictures directory and is named “Picture1.jpg”, “Picture2.jpg” etc… I really want to select the directory it gets saved too and save it as “currentdate_currenttime.jpg”?

Hi @gazzafish,

Can you please post your code where you’re performing this action? Also, please surround it with lua tags for clarity:

[lua] (your code) [/lua]

Thanks,

Brent

[lua]

local storyboard = require( “storyboard” )
local scene = storyboard.newScene()


– BEGINNING OF YOUR IMPLEMENTATION

local image, text1, text2, text3, memTimer

local centerX = display.contentCenterX
local centerY = display.contentCenterY

local _W = display.contentWidth
local _H = display.contentHeight

local bkgd = display.newImage( “carbonfiber.jpg”, centerX, centerY )
 

local function onSceneTouch( self, event )
 if event.phase == “began” then
  
  storyboard.gotoScene( “findreceipt”, “fade”, 400  )
  
  return true
 end
end

– Called when the scene’s view does not exist:
function scene:createScene( event )
 local screenGroup = self.view
 
 screenGroup:insert( bkgd )

 – image = display.newImage( “bg2.jpg”, centerX, centerY )
 – screenGroup:insert( image )
 
 – image.touch = onSceneTouch
 
 – text1 = display.newText( “take photo of receipt”, centerX, 50, native.systemFontBold, 24 )
 – text1:setFillColor( 1 )
 – screenGroup:insert( text1 )
 
 – text3 = display.newText( “Touch to continue.”, centerX, display.contentHeight - 100, native.systemFontBold, 18 )
 – text3:setFillColor( 1 ); text3.isVisible = false
 – screenGroup:insert( text3 )
 
 print( “\n2: createScene event” )
end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )

 – Camera not supported on simulator.                   
 local isXcodeSimulator = “iPhone Simulator” == system.getInfo(“model”)
 if (isXcodeSimulator) then
   local alert = native.showAlert( “Information”, “Camera API not available on iOS Simulator.”, { “OK”})   
 end

 –
 – local bkgd = display.newRect( centerX, centerY, _W, _H )
 – :setFillColor( 0.5, 0, 0 )

 local text = display.newText( “Tap anywhere to launch Cameras”, centerX, centerY, nil, 16 )

 – local sessionComplete = function(event) 
  – local image = event.target

  – print( "Camera ", ( image and “returned an image” ) or “session was cancelled” )
  – print( "event name: " … event.name )
  – print( "target: " … tostring( image ) )

  – if image then
   – -- display.capture( displayObject [, saveToPhotoLibraryFlag] )

   – -- center image on screen
   – image.x = centerX
   – image.y = centerY
   – local w = image.width
   – local h = image.height
   – print( “w,h = “… w …”,” … h )
  – end
 – end

local sessionComplete = function(event)

 local photo = event.target
 local photoGroup = display.newGroup() 
 photoGroup:insert(photo)

 local myDirectory = system.TemporaryDirectory --or system.DocumentsDirectory
 display.save(photoGroup:insert, “myPhotoName.jpg”, myDirectory)

 --then do your multipart data stuff using:
 multipart:addFile(“myFile”, system.pathForFile( “myPhotoName.jpg”, myDirectory ), “image/jpeg”, “myPhotoName.jpg”)

end
 
 local listener = function( event )
  if media.hasSource( media.Camera ) then
   media.show( media.Camera, sessionComplete )
  
  else
   native.showAlert(“Corona”, “Camera not found”)
  end
  return true
 end
 bkgd:addEventListener( “tap”, listener )
end

– Called when scene is about to move offscreen:
function scene:exitScene()
 
 print( “2: exitScene event” )
 
 – remove touch listener for image
 image:removeEventListener( “touch”, image )
 
end

– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )
 
 print( “((destroying scene 2’s view))” )
end


– END OF YOUR IMPLEMENTATION

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )


return scene

[/lua]

cheers

Hi @gazzafish,

Are you having issues with the “display.save()” call on line 76 of the posted code? There’s definitely a syntax error in that: you tell it to save “photoGroup:insert” which isn’t a reference to the photo object or the group. That would be the first thing to correct.

Take care,

Brent

Hi @gazzafish,

Can you please post your code where you’re performing this action? Also, please surround it with lua tags for clarity:

[lua] (your code) [/lua]

Thanks,

Brent

[lua]

local storyboard = require( “storyboard” )
local scene = storyboard.newScene()


– BEGINNING OF YOUR IMPLEMENTATION

local image, text1, text2, text3, memTimer

local centerX = display.contentCenterX
local centerY = display.contentCenterY

local _W = display.contentWidth
local _H = display.contentHeight

local bkgd = display.newImage( “carbonfiber.jpg”, centerX, centerY )
 

local function onSceneTouch( self, event )
 if event.phase == “began” then
  
  storyboard.gotoScene( “findreceipt”, “fade”, 400  )
  
  return true
 end
end

– Called when the scene’s view does not exist:
function scene:createScene( event )
 local screenGroup = self.view
 
 screenGroup:insert( bkgd )

 – image = display.newImage( “bg2.jpg”, centerX, centerY )
 – screenGroup:insert( image )
 
 – image.touch = onSceneTouch
 
 – text1 = display.newText( “take photo of receipt”, centerX, 50, native.systemFontBold, 24 )
 – text1:setFillColor( 1 )
 – screenGroup:insert( text1 )
 
 – text3 = display.newText( “Touch to continue.”, centerX, display.contentHeight - 100, native.systemFontBold, 18 )
 – text3:setFillColor( 1 ); text3.isVisible = false
 – screenGroup:insert( text3 )
 
 print( “\n2: createScene event” )
end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )

 – Camera not supported on simulator.                   
 local isXcodeSimulator = “iPhone Simulator” == system.getInfo(“model”)
 if (isXcodeSimulator) then
   local alert = native.showAlert( “Information”, “Camera API not available on iOS Simulator.”, { “OK”})   
 end

 –
 – local bkgd = display.newRect( centerX, centerY, _W, _H )
 – :setFillColor( 0.5, 0, 0 )

 local text = display.newText( “Tap anywhere to launch Cameras”, centerX, centerY, nil, 16 )

 – local sessionComplete = function(event) 
  – local image = event.target

  – print( "Camera ", ( image and “returned an image” ) or “session was cancelled” )
  – print( "event name: " … event.name )
  – print( "target: " … tostring( image ) )

  – if image then
   – -- display.capture( displayObject [, saveToPhotoLibraryFlag] )

   – -- center image on screen
   – image.x = centerX
   – image.y = centerY
   – local w = image.width
   – local h = image.height
   – print( “w,h = “… w …”,” … h )
  – end
 – end

local sessionComplete = function(event)

 local photo = event.target
 local photoGroup = display.newGroup() 
 photoGroup:insert(photo)

 local myDirectory = system.TemporaryDirectory --or system.DocumentsDirectory
 display.save(photoGroup:insert, “myPhotoName.jpg”, myDirectory)

 --then do your multipart data stuff using:
 multipart:addFile(“myFile”, system.pathForFile( “myPhotoName.jpg”, myDirectory ), “image/jpeg”, “myPhotoName.jpg”)

end
 
 local listener = function( event )
  if media.hasSource( media.Camera ) then
   media.show( media.Camera, sessionComplete )
  
  else
   native.showAlert(“Corona”, “Camera not found”)
  end
  return true
 end
 bkgd:addEventListener( “tap”, listener )
end

– Called when scene is about to move offscreen:
function scene:exitScene()
 
 print( “2: exitScene event” )
 
 – remove touch listener for image
 image:removeEventListener( “touch”, image )
 
end

– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )
 
 print( “((destroying scene 2’s view))” )
end


– END OF YOUR IMPLEMENTATION

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )


return scene

[/lua]

cheers

Hi @gazzafish,

Are you having issues with the “display.save()” call on line 76 of the posted code? There’s definitely a syntax error in that: you tell it to save “photoGroup:insert” which isn’t a reference to the photo object or the group. That would be the first thing to correct.

Take care,

Brent