Android 有没有一种简单的方法可以读取一个特定文件夹中的所有*.png文件?

Android 有没有一种简单的方法可以读取一个特定文件夹中的所有*.png文件?,android,file,listview,android-arrayadapter,Android,File,Listview,Android Arrayadapter,我已经找到了很多方法来实现这一点,但都不简单(我无法实现更复杂的方法,所以我正在寻找一个更简单的解决方案)。我想用我的目录中所有png文件的名称填充我的ArrayAdapter,我用来保存应用程序中的一些png。 这个很棒:http://www.dreamincode.net/forums/topic/190013-creating-simple-file-chooser/ 但这并不容易。我相信有一个更简单的方法,我就是找不到它(我真的做了我的家庭作业,并且一直在寻找它)。我假设像File.li

我已经找到了很多方法来实现这一点,但都不简单(我无法实现更复杂的方法,所以我正在寻找一个更简单的解决方案)。我想用我的目录中所有png文件的名称填充我的ArrayAdapter,我用来保存应用程序中的一些png。 这个很棒:http://www.dreamincode.net/forums/topic/190013-creating-simple-file-chooser/ 但这并不容易。我相信有一个更简单的方法,我就是找不到它(我真的做了我的家庭作业,并且一直在寻找它)。我假设像File.listFiles()这样的东西可以工作,我只是不知道如何工作

我的代码:

    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    String[] values = new String[] ;

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
    getListView().setBackgroundDrawable(getResources().getDrawable(R.drawable.diver));
}

@Override

protected void onListItemClick(ListView list, View v, int position, long id) {
    list.setBackgroundDrawable(getResources().getDrawable(R.drawable.diver));
    String item = (String) getListAdapter().getItem(position);

    setContentView(R.layout.table);
    myWebView = (WebView)findViewById(R.id.myWebView);
    myWebView.setInitialScale(50);
    myWebView.getSettings().setBuiltInZoomControls(true);
    myWebView.loadUrl(Environment.getExternalStorageDirectory()+ File.separator + "DivePlanner" + File.separator + item);


}
public void onCreate(捆绑冰柱){
超级冰柱;
this.requestWindowFeature(Window.FEATURE\u NO\u TITLE);
字符串[]值=新字符串[];
ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1的值);
setListAdapter(适配器);
getListView().setBackgroundDrawable(getResources().getDrawable(R.drawable.diver));
}
@凌驾
受保护的void onListItemClick(列表视图列表、视图v、整数位置、长id){
list.setBackgroundDrawable(getResources().getDrawable(R.drawable.diver));
字符串项=(字符串)getListAdapter().getItem(位置);
setContentView(R.layout.table);
myWebView=(WebView)findViewById(R.id.myWebView);
myWebView.setInitialScale(50);
myWebView.getSettings().SetBuilTinZoomControl(true);
myWebView.loadUrl(Environment.getExternalStorageDirectory()+File.separator+“DivePlanner”+File.separator+项);
}

我需要用目录/sdcard/DivePlanner/中所有png文件的名称填充值[]。如果有人知道任何简单的方法,非常感谢

是的,您应该使用File.listFiles()方法从所需文件夹获取所有文件。当您有一个文件数组时,您应该简单地遍历它,并从每个文件中检索其名称

如果您只需要.png文件,请检查java FileFilter类及其用法

为了方便起见,请看这里:


您可以使用以下内容:

final File directory = new File(path);
if(!directory.exists() || !directory.isDirectory()){
    //do something
}
final Collection<String> pngFilenames = new HashSet<String>();
for(final String filename : directory.list()){
    if(filename.endsWith(".png")){
        pngFilenames.add(filename);
    }
}
final String[] values = (String[])pngFilenames.toArray(new String[pngFilenames.size()]);
final File directory=新文件(路径);
如果(!directory.exists()| |!directory.isDirectory()){
//做点什么
}
最终集合pngFilenames=new HashSet();
for(最终字符串文件名:directory.list()){
if(filename.endsWith(“.png”)){
pngFilenames.add(文件名);
}
}
最终字符串[]值=(字符串[])pngFilenames.toArray(新字符串[pngFilenames.size());