Skip to main content
Inspiring
May 15, 2011
Question

hello world camera roll example

  • May 15, 2011
  • 1 reply
  • 7575 views

I just got cs5.5 I'm looking for a very simple camera roll hello world type example where i can just press a button to load the camera roll and add an image into the app.

Does anyone have one or know of one?

Once it is in the app can you use a shared object to save the image for the next time the app loads?

This topic has been closed for replies.

1 reply

joeboy_ukAuthor
Inspiring
May 16, 2011

Thanks for that, using the last example I managed to create an app that on the press on a button it will load the camera roll and when an image is selected it closes it, but what do I need to add to this code to load the image that is selected into a movieclip and have it save for the next time i load the app?

import flash.events.Event;

import flash.events.MediaEvent;

import flash.media.CameraRoll;

var _roll:CameraRoll;

loadCamera.addEventListener(MouseEvent.CLICK, _launchCamera);

function _launchCamera(evt:Event=null):void

{

if (CameraRoll.supportsBrowseForImage)

{

_roll = new CameraRoll();

_roll.browseForImage();

}

}

function _onCancel(event:Event):void

{

_removeRollListeners();

}

function _onSelect(event:MediaEvent):void

{

_removeRollListeners();

}

function _addRollListeners():void

{

if (_roll)

{

_roll.addEventListener(MediaEvent.SELECT, _onSelect);

_roll.addEventListener(Event.CANCEL, _onCancel);

}

}

function _removeRollListeners():void

{

if (_roll)

{

_roll.removeEventListener(MediaEvent.SELECT, _onSelect);

_roll.removeEventListener(Event.CANCEL, _onCancel);

}

}

Known Participant
May 16, 2011

Talam !

package {
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.MediaEvent;
    import flash.media.CameraRoll;
    import flash.media.MediaPromise;
    import flash.text.TextField;
   
    import modules.utils.FPSCounter;
   
    /**
     * ...
     * @author Michaël Chartrand
     */
    public class Main extends Sprite {
       
        private var _tf:TextField;
        private var _fps:FPSCounter;
       
        private var _roll:CameraRoll;
        private var _load:Loader;
       
        public function Main():void {
            //setup stage
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;

            //add handlers
            addEventListener(Event.ADDED_TO_STAGE, init);
        }
       
        private function init(event:Event):void{
            //removes listener
            removeEventListener(Event.ADDED_TO_STAGE, init);
           
            _tf = new TextField();
            _tf.y = 25;
            _tf.text = "Mon texte";
            _tf.width = 480;
            _tf.height = 775;
            _tf.multiline = true;
            addChild(_tf);
           
            _fps = new FPSCounter();
            addChild(_fps);
           
            launchCamera();
               
        }
       
        private function launchCamera():void{
            if(CameraRoll.supportsBrowseForImage){
                    _tf.text = "Camera Roll Supported";

                    _roll = new CameraRoll();
                    _roll.browseForImage();
                    _roll.addEventListener(MediaEvent.SELECT, onSelect);
                    _roll.addEventListener(Event.CANCEL, onCancel);
            } else {
                    _tf.text = "Camera Roll Not Supported";
            }
        }
       
        private function onSelect(event:MediaEvent):void{
            _roll.removeEventListener(MediaEvent.SELECT, onSelect);
            _roll.removeEventListener(Event.CANCEL, onCancel);
           
            var imagePromise:MediaPromise = event.data;
           
            _load = new Loader();
           
            if( imagePromise.isAsync ){
                _load.contentLoaderInfo.addEventListener( Event.COMPLETE, imageLoaded );
                _load.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, imageLoadFailed );
                _load.loadFilePromise(imagePromise);
            }else{
                _load.loadFilePromise( imagePromise );
                this.addChild( _load );
            }
        }
       
        private function imageLoaded( event:Event ):void{
            _tf.appendText( "Image loaded asynchronously." );
            this.addChild( _load );
        }

        private function imageLoadFailed( event:Event ):void{
            _tf.appendText( "Image load failed." );
        }
       
       
        private function onCancel(event:Event):void{
            _roll.removeEventListener(MediaEvent.SELECT, onSelect);
            _roll.removeEventListener(Event.CANCEL, onCancel);
        }

       
    }
   
}