问题
Looking to be able to have access to browse and select a file from the users phone using as3 air.
This code only pops up an upload box and says "No Files were found". But I know I have a zip on my phone so it must not be choosing the right folder path. How can I change that to make it access the entire phone?
function unzip_init():void{
fileFilter = new FileFilter("selected your zip file.", "*.zip; *.gz2; *.bz2;");
file = new File();
//file.browseForDirectory("Choose a directory"); // Only got me the directory path
file.browseForOpen("Open", [fileFilter]);
file.addEventListener(Event.SELECT, onSelectedFile);
}
回答1:
To get all the mp3 files from the SD card
var ROOT:File = File.documentsDirectory.resolvePath("/sdcard/");
var FILES:Array = ROOT.getDirectoryListing();
var TrackN:int;
var SUBFOLDER:String;
for (var i:int = 0; i < FILES.length; i++)
{
var File_Ext:String;
File_Ext = "" + FILES[i].extension;
if (File_Ext.toLowerCase() == "mp3")
{
TrackN++;
list.addItem( { label:FILES[i].name, data:TrackN, Song:FILES[i].url} );
}
if (FILES[i].isDirectory == true)
{
SUBFOLDER = "" + FILES[i].nativePath;
getSubfolders();
}
function getSubfolders()
{
var SUBF:File = File.documentsDirectory.resolvePath(SUBFOLDER);
var FLIST:Array = SUBF.getDirectoryListing();
for (var s:int = 0; s < FLIST.length; s++)
{
File_Ext = "" + FLIST[s].extension;
if (File_Ext.toLowerCase() == "mp3")
{
TrackN++;
list.addItem( { label:FLIST[s].name, data:TrackN, Song:FLIST[s].url} );
}
if (FLIST[s].isDirectory == true)
{
SUBFOLDER = "" + FLIST[s].nativePath;
getSubfolders();
}
}
}
}
回答2:
I gave you an answer here: as3 get zip file on phone from app - file path then unzip
Basically you have to write your own file browser, help documentation suggests this (however they could built it in). e.g.
var rootDirs:Array = File.getRootFirectories();//all available root dirs
then you can pick one and lists it's content
if(rootDirs && rootDirs.length > 0)
{
var dir:File = rootDirs[0]
if(dir.isDirectory)
{
//try to enumerate it's content
var files:Array = dir.getDirectoryListing();
}
}
best regards
来源:https://stackoverflow.com/questions/12255418/as3-air-browse-for-file-on-sd-card