下面的代码下载使用AS3 Android的AIR服务器的MP3到手机上。 我想在应用程序,所以我有以下问题以供以后使用这些文件:
我怎样才能使它所以将文件保存到特定文件夹的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);
也将是甜蜜的监测这一进展,它启动时,停止进步,但我认为一个事件监听可能与这项工作。
下面的代码从下载远程URL一个mp3,使一个名为0.007文件夹(你可以更改名称或添加许多或任何文件夹)。 然后将其保存到该位置。
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记得授予正确的权限使用的是Android在APP
我到处找关于如何下载等一个答案的地方...
我个人比较喜欢的是下载从Greenshocks AS3库LoaderMax任何文件(其中已包括在自踢屁股轻便装载机我的大部分项目。
而不是指定URL是本地I指定URL是远程的还是让我..你展示一些代码:
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();
}
以下是有关完整的事件处理程序。 您也可以处理程序错误,失败,进度等你的名字..
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();
}
记住LoaderMax并不真正关心该文件是否是本地的还是远程的,它只是加载到内存中..
从这一点上,你可以决定你是否想将其保存为一个文件或只是使用它,然后将其丢弃。
如果你想将它保存为这里的文件是如何:(下文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();