how to indicate when media.show was closed and no picture chosen

hi,

i’m doing an app and i have a question i’m using

media.show function to open the media.Camera and choose a picture

and i’m also using it to open the picture gallery,

how can i recognize when the user has pressed cancel on the camera or cancel on the import from gallery,

is there an error i get somewhere ? or somthing ?

thanks

shay

Hi shay,

If you inspect the documentation on this:

http://docs.coronalabs.com/api/library/media/show.html

Notice the following line under the “listener” parameter:

“The property will be nil if the user canceled the camera or photo selection.”

Using this, you should be able to detect that and take the appropriate action in code.

Take care,

Brent

Like Brent said, in your listener, something like:

[lua]

local function cameraDone(event)

    print(" – cameraDone(event) entered.")
    
    if( event.target ~= nil ) then      – nil == no photo…
        local photo = event.target     – Otherwise, it’s the captured image…

        print("-- photo captured - w,h == ", photo.width, photo.height)

    else

        print(" – Photo cancelled")

    end

end

[/lua]

hi, 

thank you !

the problem is the event.target is nil even When i choose a picture…

could it be ?

maybe it’s becouse i’m using the comand with a path already ?

media.show( media.Camera, cameraActive, { baseDir=system.DocumentsDirectory, filename=“camera.jpg” , type=“image” } )

?

Hi Shay,

Are you developing this for iOS or Android?

offcures android :slight_smile:

ios doesn’t have a back option …

Hi Shay,

Maybe this is a silly question, but I assume you enabled both the camera and “write external storage” permissions so Android can properly use this API?

Brent

I can confirm this bug also - it always returns nil…my workaround was instead of displaying the image, save it to a file. Then do a ‘if file exists’ check, if it doesn’t, then it was cnx’d, otherwise display it on screen.

It’s not a bug.  If you call media.show() and have the selected photo save to file, then “event.target” will always be nil.

You need to use the “event.completed” property instead.

That property will be set to “true” if the end-user has selected a photo.

And this is why the documentation needs indepth updating, as this is not explained at all…i’ve not even read about event.completed until now…but thanks for the info.

Hi @sean84,

I now have this on my list to update (the documentation on it). It definitely needs to be updated a.s.a.p.

Thanks,

Brent

Hi shay,

If you inspect the documentation on this:

http://docs.coronalabs.com/api/library/media/show.html

Notice the following line under the “listener” parameter:

“The property will be nil if the user canceled the camera or photo selection.”

Using this, you should be able to detect that and take the appropriate action in code.

Take care,

Brent

Like Brent said, in your listener, something like:

[lua]

local function cameraDone(event)

    print(" – cameraDone(event) entered.")
    
    if( event.target ~= nil ) then      – nil == no photo…
        local photo = event.target     – Otherwise, it’s the captured image…

        print("-- photo captured - w,h == ", photo.width, photo.height)

    else

        print(" – Photo cancelled")

    end

end

[/lua]

hi, 

thank you !

the problem is the event.target is nil even When i choose a picture…

could it be ?

maybe it’s becouse i’m using the comand with a path already ?

media.show( media.Camera, cameraActive, { baseDir=system.DocumentsDirectory, filename=“camera.jpg” , type=“image” } )

?

Hi Shay,

Are you developing this for iOS or Android?

offcures android :slight_smile:

ios doesn’t have a back option …

Hi Shay,

Maybe this is a silly question, but I assume you enabled both the camera and “write external storage” permissions so Android can properly use this API?

Brent

I can confirm this bug also - it always returns nil…my workaround was instead of displaying the image, save it to a file. Then do a ‘if file exists’ check, if it doesn’t, then it was cnx’d, otherwise display it on screen.

It’s not a bug.  If you call media.show() and have the selected photo save to file, then “event.target” will always be nil.

You need to use the “event.completed” property instead.

That property will be set to “true” if the end-user has selected a photo.

And this is why the documentation needs indepth updating, as this is not explained at all…i’ve not even read about event.completed until now…but thanks for the info.