English 中文(简体)
是否有一种简单的方法可以在一个特定文件夹中读取全部 *.png 文件?
原标题:Is there a simple way to read all *.png files in ONE specific folder?

I have found plenty of ways to do that, none of them simple, though (and I wasn t able to implement the more complicated one, so I am looking for a simpler solution). I would like to fill my ArrayAdapter with the names of all png files in my direcory I use for saving some pngs from the app. This one is great:http://www.dreamincode.net/forums/topic/190013-creating-simple-file-chooser/ but not exactly easy. I believe there is a simpler way, I just couldn t find it (and I really did my homework and was looking for it quite some time). I assume something like File.listFiles() could work, I just don t know how.

我的代码:

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


}

我要填入目录 / sdcard/ DivePlanner / 中所有 png 文件的名称。 如果有人知道任何容易的方法, 非常感谢!

问题回答

是, 您应该使用 File. listFiles () 方法从想要的文件夹中获取所有文件。 当您有一系列文件时, 您应该简单地通过它进行循环, 从每个文件取回文件的名称 。

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

你们的集会,你们看这里:

http://www.exampledepote.com/egs/java.io/GetFiles.html

你可以使用这样的东西:

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




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签