title: Flex 4 - How to store image data into variable or object
body: <p>My coding,</p>
  
  <pre><code>    &lt;fx:Script&gt;
      &lt;![CDATA[
          private var loadFile:FileReference;
          protected function browseButtonClickHandler(event:MouseEvent):void
          {
              loadFile = new FileReference();
              loadFile.addEventListener(Event.SELECT, fileSelectHandler);
              var fileFilter:FileFilter = new FileFilter("Images: (*.jpeg, *.jpg, *.gif, *.png)", "*.jpeg; *.jpg; *.gif; *.png");
              loadFile.browse([fileFilter]);
          }
  
          protected function fileSelectHandler(event:Event):void
          {
              loadFile.load();
              urlInput.text = event.target.name;
          }
          public function addButtonClickEvent(event:MouseEvent):void
          {
              if (urlInput.text.length == 0)
              {
                  var num:int = gender.selectedIndex;
                  var img:ByteArray = new ByteArray();
                  var url:URLRequest = new URLRequest("http://localhost/demo/images/image.jpg");
                  var loader:Loader = new Loader();
                  loader.load(url);
                  if (num == 0)
                  {
  
                      urlInput.text = "male.jpg";
                      img = url.data; 
                  }
              } 
              else
              {
                  img = loadFile.data;
              }
              empDetails.addItem({
                  name : nameInput.text,
                  image : img
              });
          }
  
      ]]&gt;
  &lt;/fx:Script&gt;
  &lt;s:Form id="addForm"&gt;
      &lt;s:FormItem id="nameLabel" label="Name"&gt;
          &lt;s:TextInput  id="nameInput"/&gt;
      &lt;/s:FormItem&gt;
      &lt;s:FormItem id="urlLabel" label="Employee Image:"&gt;
          &lt;s:HGroup&gt;
              &lt;s:TextInput id="urlInput" editable="false"/&gt;
              &lt;s:Button id="browseButton" label="Browse" click="browseButtonClickHandler(event)"/&gt;
          &lt;/s:HGroup&gt;
      &lt;/s:FormItem&gt;
      &lt;s:FormItem&gt;
          &lt;s:HGroup&gt;
              &lt;s:Button id="addButton" label="Add" click="addButtonClickEvent(event)"/&gt;
          &lt;/s:HGroup&gt;
      &lt;/s:FormItem&gt;
  &lt;/s:Form&gt;
  </code></pre>
  
  <p>Here, I have to add the name, and the image into the ArrayCollection for future use. If I doesn't select image here, it will throw an exception. But what I need, If I doesn't select image in form, it has to be take this image "image.jpg" as a selected image. But an error is being here like
  <em>Multiple markers at this line:
  -1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.utils:ByteArray.
  -img</em>
  I don't know how to remove this error. If anybody can find it, Please inform me? or How can I do this task?</p>