我想在我的应用中显示SD卡中可用的音乐文件列表。这是java代码。 代码工作没有任何错误,但没有显示MP3文件而是ListView正在填充手机内存中文件夹的名称。
MainActivity.java
class AppController < UIViewController
def viewDidLoad
view.backgroundColor = UIColor.blackColor
@label = UILabel.alloc.initWithFrame( CGRectMake(100,100,200,50) )
@label.textColor = UIColor.whiteColor
@label.text = "Handling device orientation"
view.addSubview(@label)
end
def ipad?
return true if UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad
end
def shouldAutorotate
false
end
def supportedInterfaceOrientations
if ipad?
UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight
else
UIInterfaceOrientationMaskPortrait
end
end
def preferredInterfaceOrientationForPresentation
ipad? ? UIInterfaceOrientationMaskLandscapeLeft : UIInterfaceOrientationMaskPortrait
end
end
AudioFileFilter.java
final String MEDIA_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ListView listsong =(ListView)findViewById(R.id.songslistView);
ArrayList<String> mp3list = new ArrayList<String>();
mp3list=mp3select();
Toast.makeText(this, "mp3files" + mp3list, Toast.LENGTH_LONG).show();
ArrayAdapter<String> listAdapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,mp3list);
listsong.setAdapter(listAdapter);
}
public ArrayList<String> mp3select(){
File home = new File(MEDIA_PATH);
ArrayList<String> list = new ArrayList<String>();
//if (home.isDirectory())
if (home.listFiles(new AudioFileFilter()).length > 0)
{
for (File file : home.listFiles(new AudioFileFilter()))
{
list.add(file.getName());
}
}
return list;
}
我已经解决了与该主题相关的几个问题。我无法确定问题所在。任何帮助表示赞赏。在此先感谢。
答案 0 :(得分:1)
您没有通过这些目录进行递归,而只是在您将allowDirectories传递给AudioFilter时只显示目录名称,因为您传递的是无参数构造函数默认为true。您要执行的操作扫描目录以查找与您的过滤器匹配的文件,当您找到子目录时,您希望将其添加到要扫描的目录列表中。你可以通过多种方式实现这一目标。一种方法是递归。找到目录时,将该目录作为输入参数调用相同的函数。另一种方法是您可以保留要扫描的目录列表,每次找到目录时,都将其附加到列表中。完成扫描目录中的文件后,从目录列表中弹出一个目录并继续,直到您没有要扫描的目录。
这是递归方式的想法:
private FileFilter mFileFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mFileFilter = new AudioFilter();
ListView listsong =(ListView)findViewById(R.id.songslistView);
ArrayList<String> mp3list = new ArrayList<String>();
ArrayList<File> fileList = new ArrayList<File>();
File home = new File(MEDIA_PATH);
mp3select(home, fileList);
//now you need to map your ArrayList<File> to ArrayList<String>. Personally I would use a different kind of adapter and just return the name of the file while keeping the entire file reference
ArrayAdapter<String> listAdapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,mp3list);
listsong.setAdapter(listAdapter);
}
public void mp3select(File dir, ArrayList<File> fileList){
if (!home.isDirectory()) {
return;
}
for (File file : home.listFiles(mAudioFilter))
{
if (file.isDirectory()) {
mp3List(file, fileList);
} else {
fileList.add(file);
}
}
}