Saving screen shot to share?

Am hoping there is a easier solution to this.

At the moment I save screen shots using display.capture but now I need to add the ability to share this screen shot via social media.

The trouble is I don’t know the file name, so am thinking of swapping over to display.save

display.save( group, { filename=“entireGroup.png” } )

Then load the file back in to display it and also ready for sharing.

What should I be using for a filename if I don’t want to overwrite the file each time?

It’s a pity display.capture doesn’t return the filename, that would be easier.

Any tips welcome.

Thanks,

Dave

The way the OS sandboxes things, it’s not just a matter of grabbing a file name from the photo library. You would need to bring up a photo picker, select the screen shot and then have that save the image to your apps sandbox.  Using display.save() is the best for this.

As far as a filename goes, you could use os.date() to get the year, month, day, hour, min (I don’t think you would need second resolution) and construct a name like: appname201608251115.png. At that point you have a predictable file name.

You could store and save an index number in your apps settings and do:  appname_indexnumber.png and increment the index number each time. This is the say camera’s save their files:  DSC_8394.jpg for instance.

One other good option is to use the system timer.

filename = “appname_” … tostring( math.floor( system.getTimer() ) … “.png”

See: https://docs.coronalabs.com/api/library/system/getTimer.html

Rob

You can use this:

Create the screenshot variable

[lua]

local screenshot

[/lua]

[lua]

local function captureScreenshot(group)

    screenshot = “screenshot_” …os.date("%Y%m%d%H%M%S") … “.png”

    display.save( group, { filename=screenshot, baseDir=system.DocumentsDirectory, captureOffscreenArea=false, backgroundColor={0,0,0,0} } )

    print(“Screenshot saved”)

end

[/lua]

If you’re using Composer, you can place the following file inside scene:show(event)

[lua]

    local function screenshotListener(event)

        captureScreenshot(sceneGroup)

        return true

    end

    

    timer.performWithDelay( 3000, screenshotListener )

[/lua]

Now, inside your share function:

[lua]

image = 

                {

                    { filename = screenshot, baseDir = system.DocumentsDirectory },

                },

[/lua]

Thanks for this, really helps.

If I save in the Documents directory will the images still be viewable in the Photos app?

That was the good thing about display.capture in that it went into the photos folder (i think).

Dave

No, it won’t be viewable inside the Photos app. The Documents directory resides within the app folder itself.

You might want to consider continuing to use display.capture() and use the Activity (iOS) and Social (Android) plugins and let them share it themselves from the photo album.

Rob

Hi Rob,

I am using the Activity plugin to share it but from reading your blog post you have to supply it a image file. Am i missing something?

Dave

Yea, good point.

Right so I came up with -

Using the code above to save a copy of the image in the TemporaryDirectory

Then keep using display.capture to also save a copy in the picture library.

That way I have a copy in the picture library for the user and a copy in a temp directory, which I know the filename of for sharing.

Dave

The way the OS sandboxes things, it’s not just a matter of grabbing a file name from the photo library. You would need to bring up a photo picker, select the screen shot and then have that save the image to your apps sandbox.  Using display.save() is the best for this.

As far as a filename goes, you could use os.date() to get the year, month, day, hour, min (I don’t think you would need second resolution) and construct a name like: appname201608251115.png. At that point you have a predictable file name.

You could store and save an index number in your apps settings and do:  appname_indexnumber.png and increment the index number each time. This is the say camera’s save their files:  DSC_8394.jpg for instance.

One other good option is to use the system timer.

filename = “appname_” … tostring( math.floor( system.getTimer() ) … “.png”

See: https://docs.coronalabs.com/api/library/system/getTimer.html

Rob

You can use this:

Create the screenshot variable

[lua]

local screenshot

[/lua]

[lua]

local function captureScreenshot(group)

    screenshot = “screenshot_” …os.date("%Y%m%d%H%M%S") … “.png”

    display.save( group, { filename=screenshot, baseDir=system.DocumentsDirectory, captureOffscreenArea=false, backgroundColor={0,0,0,0} } )

    print(“Screenshot saved”)

end

[/lua]

If you’re using Composer, you can place the following file inside scene:show(event)

[lua]

    local function screenshotListener(event)

        captureScreenshot(sceneGroup)

        return true

    end

    

    timer.performWithDelay( 3000, screenshotListener )

[/lua]

Now, inside your share function:

[lua]

image = 

                {

                    { filename = screenshot, baseDir = system.DocumentsDirectory },

                },

[/lua]

Thanks for this, really helps.

If I save in the Documents directory will the images still be viewable in the Photos app?

That was the good thing about display.capture in that it went into the photos folder (i think).

Dave

No, it won’t be viewable inside the Photos app. The Documents directory resides within the app folder itself.

You might want to consider continuing to use display.capture() and use the Activity (iOS) and Social (Android) plugins and let them share it themselves from the photo album.

Rob

Hi Rob,

I am using the Activity plugin to share it but from reading your blog post you have to supply it a image file. Am i missing something?

Dave

Yea, good point.

Right so I came up with -

Using the code above to save a copy of the image in the TemporaryDirectory

Then keep using display.capture to also save a copy in the picture library.

That way I have a copy in the picture library for the user and a copy in a temp directory, which I know the filename of for sharing.

Dave