3 Replies Latest reply on Oct 11, 2010 6:59 PM by odyssey27

    Convert FileReference to Bitmap

    PeakDigital Level 1

      I have successfully captured a list of image files in a FileReferenceList, and need to convert each file to a Bitmap for processing.   Here is a dump via ObjectUtil.ToString() of my files:


        fileList = (Array)#1
          [0] (flash.net::FileReference)#2
            creationDate = Thu Dec 31 17:00:00 GMT-0700 1903
            creator = (null)
            modificationDate = Thu Aug 12 08:08:58 GMT-0600 2004
            name = "image1.JPG"
            size = 793089
            type = (null)
          [1] (flash.net::FileReference)#3
            creationDate = Sat Jun 20 16:59:22 GMT-0600 2009
            creator = (null)
            modificationDate = Sat Jun 20 16:59:22 GMT-0600 2009
            name = "image2.png"
            size = 268971
            type = (null)



      Here's part of the code

       for(var i:int=0;i<theFiles.fileList.length;i++)
       {//process each media file.
       var bmdataSourceImage:BitmapData = Bitmap(theFiles.fileList[i]).bitmapData;


      The last line causes this error:

      TypeError: Error #1034: Type Coercion failed: cannot convert flash.net::FileReference@20076b01 to flash.display.Bitmap.


      What step do I need to do to convert a file from the reference list into a bitmap?




        • 1. Re: Convert FileReference to Bitmap
          paul.williams Level 4

          You need to use the new load() method of FileReference to load the file locally within the player. This method is only available in Flash Player 10. Previous players do not allow local loading of files, forcing developers to upload the file directly to the server and then download it for subsequent processing.


          If you must support FP9 and ealier then you will need to use the upload() method of FileReference to upload the file directly to the server, and then subsequently download it for any client-side processing that may be required.


          If you are happy going with an FP10-only solution and you are using Flex 3.2 or later, then you will need to modify your build to use the new FP10 features. You do this by going to Project Properties -> Flex Compiler in Flex Builder and modifying the Require Flash Player version to 10.0.0.


          Save these properties and do a clean build.


          With these settings the following demo application will build and run:


          <?xml version="1.0" encoding="utf-8"?>
                      private var fileReference : FileReference;
                      private var loader : Loader;
                      private function onLoad() : void
                          var fileFilter : FileFilter = new FileFilter( "Image", "*.jpg" );
                          fileReference = new FileReference();
                          fileReference.browse( [ fileFilter ] );
                          fileReference.addEventListener( Event.SELECT, onSelect ) ;
                      private function onSelect( event : Event ) : void
                          fileReference.load() ;
                          fileReference.addEventListener( Event.COMPLETE, onLoaded );
                      private function onLoaded( event : Event ) : void
                          bitmapImage.source = fileReference.data;

          1 person found this helpful
          • 2. Re: Convert FileReference to Bitmap
            PeakDigital Level 1

            Thank you for the explanation and sample code.


            My problem is that I'm running on an older Mac with a PowerPC processor, and I'm not sure if I can get FP10 debugger to function on my system.


            I can't upload then process. Part of the reason I am trying to do this is that my server provider has a very low timeout cap on uploads, so I need to reduce the filesize prior to uploading.


            Thanks again.


            • 3. Re: Convert FileReference to Bitmap
              odyssey27 Level 1

              Sorry to hijack, but hey Adobe! FileReferenceList does not fire the complete event against fileList fileReferences in Air 1.5, fp 10.0.0. This has been a reported bug which has been closed, submitted, closed and closed again and again. This will likely cost us about $30,000 because you refuse to acknowledge this is a real bug. Pls fix!!!!!!!!!!!!!!!!