I think you failed to read all of my message., especially the part where I say "but if you're using CameraRoll.browseForImage() to get the image". Isn't that the routine you're using to let them browse for a photo? If it is, it still returns a MediaPromise that you'll need to load into a Loader.
The issue is probably that the underlying data source of the media promise is asynchronous rather than synchronous. When you first get the media selected event, the data hasn't been read yet, so bytesAvailable is still zero. You have to add an event listener to the data source and wait for progress events or the complete event before accessing the data.
Caveat, this is how it works on Android. I don't have an iOS device to test on -- and will be at a conference this week, so I won't be able to borrow one.
Here's an example of reading the media promise data directly without using a temp file or loader as an intermediate step:
package
{
import flash.desktop.NativeApplication;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.MediaEvent;
import flash.media.CameraRoll;
import flash.media.MediaPromise;
import flash.utils.ByteArray;
import flash.utils.IDataInput;
public class CameraRollMediaPromiseExample extends Sprite
{
private var cameraRoll:CameraRoll = new CameraRoll();
public function CameraRollMediaPromiseExample()
{
this.stage.align = StageAlign.TOP_LEFT;
this.stage.scaleMode = StageScaleMode.NO_SCALE;
if( CameraRoll.supportsBrowseForImage )
{
trace( "Initializing app..." );
cameraRoll.addEventListener( MediaEvent.SELECT, imageSelected );
cameraRoll.addEventListener( Event.CANCEL, browseCanceled );
cameraRoll.addEventListener( ErrorEvent.ERROR, mediaError );
cameraRoll.browseForImage();
}
else
{
trace( "Image browse is not supported.");
}
}
private var dataSource:IDataInput;
private var eventSource:IEventDispatcher;
private function imageSelected( event:MediaEvent ):void
{
trace( "Media selected..." );
var imagePromise:MediaPromise = event.data;
dataSource = imagePromise.open();
if( imagePromise.isAsync )
{
trace( "Asynchronous media promise." );
eventSource = dataSource as IEventDispatcher;
trace( eventSource );
eventSource.addEventListener( Event.COMPLETE, onDataComplete );
}
else
{
trace( "Synchronous media promise." );
readMediaData();
}
}
private function onDataComplete( event:Event ):void
{
trace("Data load complete");
readMediaData();
}
private function browseCanceled( event:Event ):void
{
trace( "Media select canceled." );
NativeApplication.nativeApplication.exit();
}
private function readMediaData():void
{
var imageBytes:ByteArray = new ByteArray();
dataSource.readBytes( imageBytes );
//the rest of this is just testing what we actually read
trace(imageBytes.length);
imageBytes.position = 0;
var string:String = imageBytes.readUTFBytes( 300 );
trace( string );
}
private function mediaError( error:ErrorEvent ):void
{
trace( "Error:" + error.text );
NativeApplication.nativeApplication.exit();
}
}
}