从CameraRoll加载图像后出现“遇到文件结尾”

时间:2014-12-19 17:21:05

标签: actionscript-3 flash air bytearray

我使用下面的代码从CameraRoll加载图像。我明白了 readMediaData()函数启动后出错:Error #2030: End of file was encountered at flash.utils::ByteArray/readUnsignedByte()

我使用的是Adobe Air 15和iPhone 6。 EXIF包来自此处:https://github.com/bashi/exif-as3 如何修复错误?这是我的代码:

import jp.shichiseki.exif.*;

var exif;
var mediaPromise:MediaPromise;
var dataSource:IDataInput;
var loader:Loader = new Loader();
var deviceCameraRoll:CameraRoll;

function loadImageFromCameraRoll(e:Event=null):void
{
    trace("loadImageFromCameraRoll");
    deviceCameraRoll = new CameraRoll();
    deviceCameraRoll.addEventListener(MediaEvent.SELECT, onSelected);
    deviceCameraRoll.browseForImage();
}

function onSelected(e:MediaEvent)
{
    trace("onSelected");
    mediaPromise = e.data;
    dataSource = mediaPromise.open();

    if (mediaPromise.isAsync)
    {
        trace( "Asynchronous media promise." );
        var eventSource:IEventDispatcher = dataSource as IEventDispatcher;
        eventSource.addEventListener( Event.COMPLETE, onMediaLoaded );
    }
    else
    {
        trace( "Synchronous media promise." );
    }
}

function onMediaLoaded( event:Event ):void
{
    trace("Media load complete");
    readMediaData();
}



function readMediaData():void
{
    trace("readMediaData");
    loader.contentLoaderInfo.addEventListener( Event.COMPLETE, imageLoaded );
    loader.loadFilePromise(mediaPromise);

    var data:ByteArray = new ByteArray();
    dataSource.readBytes(data);
    exif = new ExifInfo(data);

    trace("ORIENTATION: "+ getOrientation(exif.ifds.primary));
}

function imageLoaded( event:Event ):void
{
    trace("Image loaded asynchronously.");
    holder.addChild(loader );
}

function getOrientation(ifd:IFD):String
{
    var str:String = "";
    for (var entry:String in ifd)
    {
        if (entry == "Orientation")
        {
            str = ifd[entry];
        }
    }
    switch (str)
    {
        case "1" ://normal
            str = "NORMAL";
            break;
        case "3" ://rotated 180 degrees (upside down)
            str = "UPSIDE_DOWN";
            break;
        case "6" ://rotated 90 degrees CW
            str = "ROTATED_LEFT";
            break;
        case "8" ://rotated 90 degrees CCW
            str = "ROTATED_RIGHT";
            break;
        case "9" ://unknown
            str = "UNKNOWN";
            break;
    }
    return str;
}

btnload.addEventListener(MouseEvent.CLICK, loadImageFromCameraRoll);