如何SD卡在ListView上显示的文件? [英] How to display files on the SD card in a ListView?

查看:173
本文介绍了如何SD卡在ListView上显示的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个点击后进入,使用从SD卡显示的所有媒体文件一类的一个按钮的ListView

I would like to create a button that when clicked will go to a class that displays all media files from an SD card using a ListView.

在从列表中选择其将返回到选择的主类文件名。如果返回的文件是一个图像文件,它会显示在的ImageView ,如果返回的文件是一个音频文件,它只会显示一个图标。

After selecting from the list it will then return the filename selected to the main class. IF the returned file is an image file, it will be displayed in an ImageView and if the returned file is an audio file, it'll just display an icon.

推荐答案

添加方法的GetFiles()你的程序。调用它来获得的ArrayList<> 的所有文件。然后,你可以用它来填充你的列表视图。您需要提供字符串参数目录路径

Add a Method GetFiles() to your program. Call it to get an ArrayList<> of all the files. You can then use it to populate your listview. You need to provide String argument DirectoryPath.

的功能:

public ArrayList<String> GetFiles(String DirectoryPath) {
    ArrayList<String> MyFiles = new ArrayList<String>();
    File f = new File(DirectoryPath);

    f.mkdirs();
    File[] files = f.listFiles();
    if (files.length == 0)
        return null;
    else {
        for (int i=0; i<files.length; i++) 
            MyFiles.add(files[i].getName());
    }

    return MyFiles;
}

用法示例:

@Override
public void onCreate() {
// Other Code

    ListView lv;
    ArrayList<String> FilesInFolder = GetFiles("/sdcard/somefolder");
    lv = (ListView)findViewById(R.id.filelist);

    lv.setAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, FilesInFolder));

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            // Clicking on items
         }
    });
}

确保外部存储器读: <一个href="http://developer.android.com/guide/topics/data/data-storage.html#filesExternal">http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

基于名称/扩展筛选文件: <一个href="http://stackoverflow.com/questions/7243991/how-to-acces-sdcard-and-return-and-array-with-files-off-a-specific-format">How以SD卡存取权限,并返回和数组的文件从一个特定的格式?

这篇关于如何SD卡在ListView上显示的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆