Question

i am trying to do an air app that take a picture than sends it to the net in post methode, and for that i need to convert an imagePromise to a byte array file... do anyone know how to do this? or anyone knows a better solution? Thank you

Était-ce utile?

La solution

The **Media**Promise - handling differs a little on iOS from e.g. Android. (Unfortunately you didn't state, which platform you are targeting)

In the most simple case, it will return you a File in mediaPromise.file Property. You can load this either by using Loader or FileStream, whichever you like most.

Listen to the MediaEvent. When it fires, you get either a file or you will have to load the mediaPromise first using Loader

Try this: (code is not error checked nor complete but should give you an idea:

private function onPictureTaken(e:MediaEvent):void
{
    // first we have to find out, if file is already saved (as on Android or BB TabletOS )
    // or has to be loaded first (like on iOS)
    var mediaPromise:MediaPromise = e.data;

    if(mediaPromise.file == null) // is iOS
    {
        trace("iOS Device found");
        // it would be a good idea to give the anonymous bitmap a name:
        var date:String = DateUtils.dateToString( new Date ,"yyyy-MM-dd_HH-mm-ss"); 
        _fileName = "Image_"  +date + "." + saveMode; // add right extension

        _loader = new Loader();
        _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onIOSImageLoadComplete);
        _loader.loadFilePromise(mediaPromise);

    }  
    else // anything else
    {
            trace("non-iOS Device found");

            file = mediaPromise.file;
           _fileName = mediaPromise.file.name;
           // you can directly access files bytes here:
            var fs:FileStream = new FileStream;
            var bytes:ByteArray = new ByteArray;
            fs.open(file , FileMode.READ);
            fs.readBytes(bytes);
            fs.close();
           // you now have your byteArray

   }
}
private function onIOSImageLoadComplete(e:Event):void
{
    // iOS handles over pictures as Bitmap, so it should to be rencoded to jpeg or png prior to uploading
    // take a look at JPEGEncoder, for example.
    // it will return a byteArray also, which you can use to upload;
    var bmp:Bitmap = _loader.content as Bitmap;
    var bytes:ByteArray = new JPEGEncoder(50).encode(bmp.bitmapData);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top