as3 android saving downloaded files from server

The following code WORKS when run on the Desktop but does not on the android device. I am thinking it has something to do with the FileStream below.

Any thoughts on how I can save this to an Android device too?

import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.net.FileReference;
import flash.net.URLRequest;
import flash.events.Event;

import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;


var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, complete_handler);
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.load(new URLRequest("http://massmediamail.com/mp3s/Why%20a%20Protestant%20Pastor%20Beacame%20Catholic.mp3"));
//any file type;


function complete_handler(event:Event):void
{
    var data:ByteArray = event.target.data;

    var fr:FileReference = new FileReference();
    trace(File.applicationDirectory.nativePath);
    fr.save(data, 'Catholic.mp3');

    var fileStream:FileStream = new FileStream();
    trace(File.applicationDirectory.nativePath);
    fileStream.open(new File (File.applicationStorageDirectory.nativePath+"\\Catholic.mp3"),FileMode.WRITE);
    fileStream.writeBytes(data, 0, data.length);
}

HERE IS THE ERROR:

    Error #2044: Unhandled IOErrorEvent:. text=Error #2038: File I/O Error.
    at Untitled_fla::MainTimeline/complete_handler()[Untitled_fla.MainTimeline::frame1:25]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()

THIS IS THE LINE IT'S REFERRING TOO:

fileStream.open(new File (File.applicationStorageDirectory.nativePath+"\\Catholic.mp3"),FileMode.WRITE);

Answers


It works with Air for Android. Yes you can do more with it later but this is the basic start.

import flash.net.FileReference;

/// It can be an mp3,jpg, png, etc... just change the url
/// and the extension name. nice huh?
var yourFileLocation = "http://YourWeb.com/YourSong.mp3";
var yourFileName = "YourSong.mp3";

var daFile:FileReference = new FileReference();
daFile.download(new URLRequest(yourFileLocation), yourFileName);

I worked FOREVER... to find this. I hope it helps many. Why is this not more common knowledge? Somethings in action-script are impossible to find sometimes.

There is ONE question I have regarding this. How can I make the code download directly to a location on the device rather than the user having to choose the location. Thanks Much!


Need Your Help

Read a record selected in DataGridView table and read through info of that record into fields that is not shown in the table

c# winforms visual-studio datagridview .net-3.5

Note: Some of the code included below has already been used in some of my other questions but regarding other issues. I have also edited most of the post to cause less confusion!

Fluent NHibernate memory leak

nhibernate memory-leaks fluent-nhibernate

I'm pulling my hair over what seems to be a memory leak but I cannot find the cause :(