×
A new build of Corona SDK is now available to subscribers. Not a subscriber? Subscribe now.
CoronaSDK 2012.821 | Released: 23 May 2012, 2:01am | What's New | Download Now

media.show( )

Description:

Opens a platform-specific interface to the device's camera or photo library. This function is asynchronous, i.e. it returns immediately so the calling code will continue to execute until the end of its scope; after that, the application will be suspended until the session is complete. By default, the image object is added to the top of the current stage. A listener is required to handle the returned display object (image).

Syntax:

media.show( mediaSource, listener )

Example:

local onComplete = function(event)
   local photo = event.target
   print( "photo w,h = " .. photo.width .. "," .. photo.height )
end
if media.hasSource( media.Camera ) then
   media.show( media.Camera, onComplete )
else
   native.showAlert( "Corona", "This device does not have a camera.", { "OK" } )
end

Parameters:

mediaSource
Can be one of the following:

  • media.PhotoLibrary
  • media.Camera
  • media.SavedPhotosAlbum

listener can be either a function listener or a table listener. If a table, it must have a completion method. The event dispatched to the listener will be a completion event with the following additional property: event.target is a display image object based on the imageSource parameter. The property will be nil if the user canceled the camera or photo selection.

Returns:

Nothing. "event.target" in the Listener function will contain the Display Image object.

Remarks:

On the Corona Simulator and Xcode Simulator, there is no camera so the API will open a finder window and allow the user to select an image file to substitute as the camera.

Note: This API (Camera and PhotoLibrary) is not support on Android devices in the current release build (591). Camera and PhotoLibrary support was added to Android starting with build 622.

Supported on operating systems and platforms for build numbers shown:
  • Mac OS X:
    Corona SDK 2.0
  • Windows:
    --
  • iOS:
    Corona SDK 2.0
  • Android:
    Build 2011.622

Replies

fusiondigital
User offline. Last seen 4 weeks 4 hours ago. Offline
Joined: 18 Apr 2011

won't work on ipad.

Calling media.show(media.PhotoLibrary, onComplete)

shows the albums/pictures dialog.
on the iPad there is no "cancel" button
Clicking outside the window, closes the albums dialog, but the application state won't change (it will stay suspended) until you re-launch the app.

any way around this?

tim@totalintegr...
User offline. Last seen 9 weeks 23 hours ago. Offline
Joined: 19 Feb 2011

+1 from me too. I'm working on a project that will need to use this feature on the iPad 2. If this hasn't been fixed yet, please add it back to your list as things have changed now that the iPad 2 has a camera. Thanks!

Tom
User offline. Last seen 11 hours 45 sec ago. Offline
Staff
Joined: 13 Jul 2010

Please post your requests and questions in the forums. As stated below, all non-document related postings will be removed.

Thanks.

fusiondigital
User offline. Last seen 4 weeks 4 hours ago. Offline
Joined: 18 Apr 2011

Already did and got no reply on the forums! I thought posting here will make sure someone reads it!

djung7
User offline. Last seen 1 day 21 hours ago. Offline
Joined: 26 Dec 2010

File it as a bug, case #6762

enfieldlaw
User offline. Last seen 2 weeks 1 day ago. Offline
Joined: 22 Jan 2011

How can I get the location information from the photos taken, or from the photo library?

Tom
User offline. Last seen 11 hours 45 sec ago. Offline
Staff
Joined: 13 Jul 2010

That information is not available in Corona.

jeremyapplebaum12
User offline. Last seen 17 hours 37 min ago. Offline
Joined: 20 Jan 2011

Is it possible to save/reference a photo taken in an app? If so how?

Thanks.

fava's picture
fava
User offline. Last seen 1 day 20 hours ago. Offline
Joined: 21 Feb 2011

+1

jeremyapplebaum12
User offline. Last seen 17 hours 37 min ago. Offline
Joined: 20 Jan 2011

Nevermind. Figured it out.

heqin.xu
User offline. Last seen 17 weeks 1 day ago. Offline
Joined: 10 May 2011

Could you please share what you have figured out?

Thanks a lot.

jeremyapplebaum12
User offline. Last seen 17 hours 37 min ago. Offline
Joined: 20 Jan 2011

Hey I would be happy to share how I did it but it would be easier for me if you showed me what you had. Also I figured out how to do it with multiple photos in a tableview, if your curious check out vAD in the created section, there should be a few promo codes left.

Thanks.

heqin.xu
User offline. Last seen 17 weeks 1 day ago. Offline
Joined: 10 May 2011

I plan to add camera function to my app,
but nothing has been done so far.

Thanks

jeremyapplebaum12
User offline. Last seen 17 hours 37 min ago. Offline
Joined: 20 Jan 2011

Ok I'm going to assume your using director and different scenes. The first thing you need to do is make a button for your camera. The function for the button should look something like this:

1
2
3
4
5
6
local function newphoto ( event )
        if (event.phase == "release") then
                media.show( media.Camera, sessionComplete )
                return true
        end
end

--Then you will need to save/target the photo. That should look something like this:

1
2
3
4
5
6
7
local function sessionComplete ( event )
        t = event.target
        local photot = display.newGroup()
        photot:insert(t)
        local baseDir = system.DocumentsDirectory
        display.save(photot, "photo.jpg", baseDir)
end

And then to load in another scene it should look something like this:

1
2
local baseDir = system.DocumentsDirectory
local img = display.newImage("photo.jpg",baseDir)

This should work, I didn't 100% test it as I took previously written code and simplified it. If there are any errors let me know.

BoarK
User offline. Last seen 1 week 9 hours ago. Offline
Joined: 20 Jan 2011

In the completion function I can detect that the event.target is nil when the user "cancels" the album/pictures dialog by touching outside the dialog box. However, I can't instantiate another media.show() dialog by calling:

media.show( media.PhotoLibrary, onComplete )

How do I cancel the previous instance of media.show() so that I can create another?

jeremyapplebaum12
User offline. Last seen 17 hours 37 min ago. Offline
Joined: 20 Jan 2011

Try adding something like:  if (t~=nil) then before t = event.target.

BoarK
User offline. Last seen 1 week 9 hours ago. Offline
Joined: 20 Jan 2011

Jeremy,

I can detect that the image (event.target) is nil. The problem lies in the fact the previous call to media.show() doesn't seem to end properly so when I try to call media.show() again the selection dialog doesn't appear on screen.

I tried reloading the scene (using director) but that didn't help. The scene change didn't even happen. Upon detection of the nil event.target I can print out a debug text but nothing more than that.

Nathan.

jeremyapplebaum12
User offline. Last seen 17 hours 37 min ago. Offline
Joined: 20 Jan 2011

I think I'm not understanding the problem correctly. Is the problem that you can't have a new call if you cancel the old one from the photo library?

Would it be possible to see some of the code your using?

BoarK
User offline. Last seen 1 week 9 hours ago. Offline
Joined: 20 Jan 2011

Yes, that's correct.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
local onComplete = function(event)
    if event.target == nil then
        -- nothing works here
        -- can't change scene
        -- can't call media.show again
        media.show( media.PhotoLibrary, onComplete )
        -- even if I use another callback function 
    else
        -- process image
    end
 
end
 
media.show( media.PhotoLibrary, onComplete )

jeremyapplebaum12
User offline. Last seen 17 hours 37 min ago. Offline
Joined: 20 Jan 2011

I think the blank media.show call is messing you up/causing your code to break. In my experience you need two functions to handle/deal with photos.

try something like:

1
2
3
4
5
6
7
8
9
10
--show/do something with the photo
local function sessionComplete ( event )
        --do something with the photo
end
 
--call the Library
local function showLibrary ( event )
                media.show( media.PhotoLibrary, sessionComplete )
                return true
end

Let me know if that works.

BoarK
User offline. Last seen 1 week 9 hours ago. Offline
Joined: 20 Jan 2011

Jeremy,

Thanks for your help. You made me take a closer look at my code. The reason for all this trouble is me calling media.show() before returning the display group to Director. I assumed that the call to media.show() is non-blocking and because Director loaded the scene properly I thought everything worked, but this isn't the case. I added a button to initiate the call to media.show() after the fact and everything worked fine.

Thanks again.

Nathan.

jeremyapplebaum12
User offline. Last seen 17 hours 37 min ago. Offline
Joined: 20 Jan 2011

Glad I could help. It took me so long to get the camera and photos to function/display the way I wanted in my most recent app.

BoarK
User offline. Last seen 1 week 9 hours ago. Offline
Joined: 20 Jan 2011

Jeremy,

I know what you mean. I am just about finished with the profile photo cropping component of my app. It's very difficult to crop images to desired dimensions for every device. I ended up doing the actual cropping on a remote server since Corona does not yet support this feature.

Nathan.

tmapps's picture
tmapps
User offline. Last seen 2 days 8 hours ago. Offline
Joined: 9 Feb 2011

is any news about Android support?? camera/gallery is a really important feature of a mobile application. Why isn't this feature on the Corona roadmap?? :-s

heqin.xu
User offline. Last seen 17 weeks 1 day ago. Offline
Joined: 10 May 2011

Does anybody know how to enable the front camera directly
for media.show()? The default one is the back camera.

Thanks!

Gloom_1
User offline. Last seen 9 weeks 1 day ago. Offline
Joined: 27 Apr 2011

Does anybody know when will add camera support on android? i want make photo on my android application, and send it via email to customer.

Tom
User offline. Last seen 11 hours 45 sec ago. Offline
Staff
Joined: 13 Jul 2010

Camera is supported on Android in the current Daily Builds. You must be a subscriber to use it.

fudesign's picture
fudesign
User offline. Last seen 46 min 2 sec ago. Offline
Joined: 19 Feb 2010

Is there anyway to load PNG files from photo library?
Since iOS screenshots are PNG format.

BTW, in build 691, media.show( media.PhotoLibrary, onComplete )
seems not working on iPhone devices (iPod touch 4th & iPhone 3GS).
Bug reported.

Thanks.

fudesign's picture
fudesign
User offline. Last seen 46 min 2 sec ago. Offline
Joined: 19 Feb 2010

correct: In build 691, you can load PNG files. However sometimes it won't allow you to resave the image to TemporaryDirectory by using:

display.save( photo, fileName..".jpg", system.TemporaryDirectory )

still trying to figure out why.

diaza56's picture
diaza56
User offline. Last seen 17 hours 43 min ago. Offline
Joined: 5 Oct 2011

@jeremyapplebaum12

Where can I find the created section? Is here on Corona? What does vAD stand for?

jeremyapplebaum12
User offline. Last seen 17 hours 37 min ago. Offline
Joined: 20 Jan 2011

What do you mean by created section? VAD stands for virtual assistant director.

diaza56's picture
diaza56
User offline. Last seen 17 hours 43 min ago. Offline
Joined: 5 Oct 2011

Its your post number 12 above

"Hey I would be happy to share how I did it but it would be easier for me if you showed me what you had. Also I figured out how to do it with multiple photos in a tableview, if your curious check out vAD in the created section, there should be a few promo codes left."

"check out vAD in the created section"

jeremyapplebaum12
User offline. Last seen 17 hours 37 min ago. Offline
Joined: 20 Jan 2011
diaza56's picture
diaza56
User offline. Last seen 17 hours 43 min ago. Offline
Joined: 5 Oct 2011

Thanks,

Looks like a great app. I am trying to finish my first here.

rupin.cdci
User offline. Last seen 20 weeks 19 hours ago. Offline
Joined: 2 Jan 2012

I am trying to build an app with real time face detection. The images/frames don't need to be captured and saved anywhere - all that's required is that faces are detected in the field of view that the camera is currently 'seeing'. Any clues on how Corona could be used to do this!?!

alabelson
User offline. Last seen 5 weeks 1 day ago. Offline
Joined: 14 May 2011

If you capture a photo from the library like this:

media.show(media.PhotoLibrary, onCompleteLibrary)

And access the photo via:

local photo = event.target

I'm able to access the width/height and display the photo on screen, but how do I get the name of the photo? Same goes for when you capture the photo via the Camera.

Thanks!!

Adam

Joshua Quick
User offline. Last seen 11 hours 12 min ago. Offline
Staff
Joined: 31 Jan 2011

Adam,

Corona does not provide the name of the photo or file name. Also, I believe iOS restricts access to the photo libraries files which may make this difficult. I believe the file name can be received on iOS 4.1 or higher, but not the older versions. In any case, I'll write this up as a feature request to be investigated later.

aidanmack's picture
aidanmack
User offline. Last seen 15 hours 22 min ago. Offline
Joined: 5 Nov 2011

Hi guys, I think I have found a bug..

In its simplest terms if you just create an app with this code init..

1
2
3
4
5
local onComplete = function(event)
   local photo = event.target
end
 
media.show( media.Camera, onComplete )

Run it on an android device

take the photo.

Photo appears in app

Lose app focus(i.e use another app.. for example take a phone call)

Go back to the above app.

The screen now just displays as "white"

I found this issue in my app googly face ... currently available on the android market.

I tried to get it on the amazon market but they failed it because of the above bug...

Joshua Quick
User offline. Last seen 11 hours 12 min ago. Offline
Staff
Joined: 31 Jan 2011

aidanmack,
This bug was fixed in daily build 707 back in December.

aidanmack's picture
aidanmack
User offline. Last seen 15 hours 22 min ago. Offline
Joined: 5 Nov 2011

Hi Joshua,
Apologies, media call works fine.
However I have narrowed down the focus bug to the scrollview widget..

Specially when the maskfile property is used.

If you use the maskfile and the app loses focus, on returning to the app everything around it goes black when you come back to it...
anyway for me to fix this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
local widget = require "widget"
local testImage =display.newImage("bgimage.jpg")
local scrollView = widget.newScrollView{
        width = 256,
        height = 320,
        hideBackground = true,
        scrollWidth = 768,
        scrollHeight = 1024,
        maskFile="mask.png",
        listener = scrollViewListener
}
                
 
local myObject = display.newImage("itemtoSroll.png" )
scrollView:insert( myObject )   

Tom
User offline. Last seen 11 hours 46 sec ago. Offline
Staff
Joined: 13 Jul 2010

Please move you questions into the forums. The documentation pages are not intended for discussion or interchange. We are slowly closing all documentation pages to comments as we transition to a new doc pages.

Thanks.