简体   繁体   English

as3 Air for Android下载和保存远程文件

[英]as3 Air for Android downloading and saving remote files

The Code below downloads an mp3 to your phone from a server using as3 Air For Android. 下面的代码使用as3 Air For Android从服务器下载mp3到手机。 I want to use these files later in the app so I have the following question: 我想稍后在应用程序中使用这些文件,所以我有以下问题:

How can I make it so the files save to a particular folder in the android app rather than a box opening for the user to choose a location? 如何将文件保存到Android应用程序中的特定文件夹而不是用于选择位置的用户框?

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);

Would also be sweet to monitor this progress, when it starts, stops and progress but I think an eventListener might work with that. 监视这个进度也很好,当它启动,停止和进行时,但我认为eventListener可能会使用它。

The Following Code downloads an mp3 from a remote url and makes a folder called .007 (You can change the name or add many or no folders). 以下代码从远程URL下载mp3并创建名为.007的文件夹(您可以更改名称或添加许多或不添加文件夹)。 Then it saves to that location. 然后它保存到该位置。

import flash.filesystem.*;
/// Change the line below to point to your mp3 online
var urlString:String = "http://YourWebsite.com/YourSound.mp3";
var urlReq:URLRequest = new URLRequest(urlString);
var urlStream:URLStream = new URLStream();
var fileData:ByteArray = new ByteArray();
urlStream.addEventListener(Event.COMPLETE, loaded);
urlStream.load(urlReq);

function loaded(event:Event):void
{
    urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
    writeAirFile();
}

function writeAirFile():void
{ 
    // Change the folder path to whatever you want plus name your mp3
    // If the folder or folders does not exist it will create it.
    var file:File = File.userDirectory.resolvePath(".007/Yahoo.mp3");
    var fileStream:FileStream = new FileStream();
    fileStream.open(file, FileMode.WRITE);
    fileStream.writeBytes(fileData, 0, fileData.length);
    fileStream.close();
    trace("The file is written.");
}

PS REMEMBER TO GRANT THE CORRECT PERMISSIONS USING ANDROID IN THE APP PS请记住在APP中使用Android的正确许可

I was looking all over the place for an answer on how to download etc... 我正在寻找各地的答案,如何下载等...

What I personally prefer is to download any file with LoaderMax from Greenshocks AS3 library (which is already included in most of my projects since its a kick-ass lightweight loader. 我个人更喜欢从Greenshocks AS3库下载任何带有LoaderMax的文件(自从它的轻量级加载器以来,它已经包含在我的大部分项目中。


Instead of specifying a URL which is local i specify a URL which is remote.. lemme show you some code: 而不是指定本地的URL我指定一个远程的URL .. lemme显示一些代码:

public function downloadTxtJpgMp3():void
{
    var queue:LoaderMax = new LoaderMax();
    emptyLoader();    //this emptys the loader from previous attempts to load any files

    queue.append( new DataLoader("http://www.70disco.com/lyrics/delegation_you_and_I.txt",{name:"test_txt" ,format:"text", estimatedBytes:4000})  );
    queue.append( new ImageLoader( "http://4.bp.blogspot.com/-WQ3uAvGdPS0/UOB_OPS6rcI/AAAAAAAAKkU/HYaXXHVHTqc/s1600/whatever-dude-whatever.jpg" , {name:"test_img" , estimatedBytes:77000, container:this, alpha:0, scaleMode:"proportionalInside"}) );
    queue.append( new MP3Loader( "http://nocturno.nsk.pt/otherpages/funny/mp3/2001-02/Cebola%20Mol/Cebola%20Mol%20-%20Satright%20No%20Chaser%20II.mp3" , {name:"test_mp3" , repeat:0, autoPlay: false}) );  

    queue.addEventListener(LoaderEvent.COMPLETE, onDownloadComplete);
    queue.load();
}

And below is the handler for the COMPLETE event. 以下是COMPLETE事件的处理程序。 You can also have handler for ERROR,FAIL,PROGRESS etc you name it.. 您也可以为ERROR,FAIL,PROGRESS等设置处理程序。

protected function onDownloadComplete(event:LoaderEvent):void
{
    var objects:Array = event.currentTarget.content ;

    var firstObjectILoaded: String      //txt
    var secondObjectILoaded:Bitmap      //jpg
    var thirdObjectILoaded: Sound       //mp3

    // ContentDisplay is found within greenshock

    firstObjectILoaded = objects[0];
    secondObjectILoaded = ((objects[1] as ContentDisplay).rawContent as Bitmap)
    thirdObjectILoaded = objects[2];

    trace(firstObjectILoaded);
    thirdObjectILoaded.play();
}   

Remember LoaderMax does not really care whether the file is local or remote, it just loads it into memory.. 记住LoaderMax并不关心文件是本地文件还是远程文件,它只是将文件加载到内存中。


From that point on you can decide whether you want to save it as a file or just use it and then discard it. 从那时起,您可以决定是将其保存为文件还是仅使用它然后丢弃它。

If you want to save it as a file here is how: (iOs example below) 如果你想将它保存为文件,请按以下方式进行:(下面的iOs示例)

var str:String = File.applicationDirectory.nativePath;

//in iOs you can save on 4 different folder directories (according to apples rules)
// cache, temp, app support, and user documents (i don't cover this below)

appCache = new File(str +"/\.\./Library/Caches");       //cache folder (in which you put files that you dont care being erased. the iOs might delete those files in case of low memory
appTempData = new File(str +"/\.\./tmp");               //temp folder (just files you temporarily want to store
appData = new File(str +"/\.\./Library/Application\ Support");      //any file your application NEEDS in order to operate, this can't be deleted by the os


var fr:FileStream = new FileStream();

fr.open(
    appTempData     //  appTempData or appCache or appData or userDocs
    .resolvePath("myCache.txt"),FileMode.WRITE); 
fr.writeUTFBytes("works");
fr.close(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM