• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
Locked
0

hello world camera roll example

Participant ,
May 15, 2011 May 15, 2011

Copy link to clipboard

Copied

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?

TOPICS
Development

Views

7.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
May 15, 2011 May 15, 2011

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 16, 2011 May 16, 2011

Copy link to clipboard

Copied

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);

}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 16, 2011 May 16, 2011

Copy link to clipboard

Copied

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);
        }

       
    }
   
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 16, 2011 May 16, 2011

Copy link to clipboard

Copied

http://forums.adobe.com/thread/848761?tstart=30

This thread covered how to save the media promise file to your application storage directory.  It also covers how to upload to a server, but that's another can of worms

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 16, 2011 May 16, 2011

Copy link to clipboard

Copied

many thanks for the code and example. I'm new to as3 and I have never used classes before. I assume I have to copy and paste that code into an .as file but what do I then do to reference the .as file from a movieclip on my main fla file so it puts the chosen image into said movieclip?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 16, 2011 May 16, 2011

Copy link to clipboard

Copied

To be honest, you are asking for something that would require a LOT of explaining, teaching, and examples if you are new to the entire coding language.

But,

In a nutshell...

To reference an external class .as file.  Go into your fla document, click your mouse anywhere on the stage that doesn't have anything on it.  Now on your properties panel you should see a "Class:" label with space to type in.  This is where you type in the name of the external class (minus the .as).  So if I have a class called myClass.as that is sitting in the same folder as my .fla file.  I would just type myClass in that.  This would then tell flash to load that class when you run that swf.

you can also have your external class call or have reference to additional classes.  In general you call your "main" class from the .fla file, and any supporting classes from that said main class.

There are other ways to incorporate external classes as well.  And you can also paste those code examples onto the timeline of the fla, but that can make for some really sloppy and disorganized coding.

You will probably struggle with this for a little bit until you get everything together and it "clicks".  If you have other specific questions, feel free to post them.  And as a hint, try to ask as specific questions as possible!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 16, 2011 May 16, 2011

Copy link to clipboard

Copied

Quite often example scripts are shown as what would be called a Document Class. In a new FLA you would look at the Property Inspector, and there's a field labelled "Class". For the above example code that would just say Main, and Main.as would be a file next to the FLA, that contains the code in it.

You don't have to work that way though, and I always like to just show timeline code examples. That way you're just concentrating on the technique, and not trying to learn about packages and classes. Here is that same code for timeline (I didn't test it, but I think it will work! I took out the FPS part because you don't have the class file for that anyway):

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;

var _tf:TextField;

var _roll:CameraRoll;

var _load:Loader;

//setup stage

stage.scaleMode = StageScaleMode.NO_SCALE;

stage.align = StageAlign.TOP_LEFT;

//add handlers

addEventListener(Event.ADDED_TO_STAGE, init);

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);

launchCamera();

}

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";

}

}

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 );

}

}

function imageLoaded( event:Event ):void {

_tf.appendText( "Image loaded asynchronously." );

this.addChild( _load );

}

function imageLoadFailed( event:Event ):void {

_tf.appendText( "Image load failed." );

}

function onCancel(event:Event):void {

_roll.removeEventListener(MediaEvent.SELECT, onSelect);

_roll.removeEventListener(Event.CANCEL, onCancel);

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
May 16, 2011 May 16, 2011

Copy link to clipboard

Copied

Here's a series of videos that cover the topic: http://tv.adobe.com/show/actionscript-11-with-doug-winnie/

Episodes 45 and up talk specifically about using classes in Flash CS5.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 17, 2011 May 17, 2011

Copy link to clipboard

Copied

Thanks for the help about classes, I think I need to do some reading 🙂

Colin, that was great I got that code to work to load my image in.

Now what can I use to save it so that when I launch the app again on iOS the image is still there?

Can I save some pathway with sharedObject and then reload it in?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 04, 2011 Jul 04, 2011

Copy link to clipboard

Copied

Hi ...

MediaEvents and MediaPromise are not in my FlashCS5 Library? ... can you tell me how to get them?, shoul I need import something else?

Thankyou and great post

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Jul 05, 2011 Jul 05, 2011

Copy link to clipboard

Copied

LATEST

These classes are included in AIR 2.5 runtime. Which version are you using?

Thanks,

Sanika

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines