How to mail a screen captured image using corona S

2019-07-03 12:06发布

问题:

I am new to corona SDK, but I've managed to capture my app scene using the following code:

local function captureArea()
    local myCaptureImage = display.captureBounds(display.currentStage.contentBounds, true)
    myCaptureImage:removeSelf()
    myCaptureImage = nil
end
bg:addEventListener("tap",captureArea)

This works perfectly.

Now I need to send the captured image(with a specific name, say: screen_1.png) to my friend via email. I've used Composing E-mail and SMS for refference, but I fail to understand how I can add this saved image in the attachment field of mail options.

Please give me a proper solution that how can I attach and send the above saved image via email.

回答1:

display.captureBounds is good for saving the whole screen to the directory. But it usually saves the file with increase in last index. So it may be difficult to read them correctly. So I prefer display.save. But it is not a straight way.

For doing this, you have to:

  • First create a localgroup.
  • Then add the screen objects to that group.
  • Return the display group
  • Use display.save to save the entire group displayed.
  • Create mail option and add attachment image from baseDirectory
  • Call mail Popup

I am giving a sample here:

-- creating the display group --
local localGroup = display.newGroup()  

-- creating display objects and adding it to the group --
local bg = display.newRect(0,0,_w,_h)
bg.x = 160
bg.y = 240
bg:setFillColor(150)
localGroup:insert(bg)

local rect = display.newRect(0,0,50,50)
rect.x = 30+math.random(260)
rect.y = 30+math.random(420)
localGroup:insert(rect)

-- Then do as follows --
local function takePhoto_andSendMail()
  -- take screen shot to baseDirectory --
  local baseDir = system.DocumentsDirectory
  display.save( localGroup, "myScreenshot.jpg", baseDir )

  -- Create mail options --
  local options =
  {
    to = { "krishnarajsalim@gmail.com",},
    subject = "My Level",
    body = "Add this...",
    attachment =
    {
      { baseDir=system.DocumentsDirectory, filename="myScreenshot.jpg", type="image" },
    },
  }

  -- Send mail --
  native.showPopup("mail", options)
end
rect:addEventListener("tap",takePhoto_andSendMail)

This will do it...

Keep coding........ :)