Android上利用ListView快速创建一个文件浏览器

381 阅读2分钟

前一片文章介绍了通过VideoView来播放视频的实例,但是是在代码里面写死的文件路径,假如有一个可以选择播放文件的文件浏览器,我们的播放器就看起来更加的正式。这次通过ListView来快速创建一个文件浏览器。

布局文件

首先在layout中创建一个activity_list_file.xml的布局文件,它里面就一个ListView控件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

ListViewActivity

创建一个ListFileActivity,它继承自ListActivity,然后加载activity_list_file作为它的View。并且接受从intent过来的一个path作为它的加载文件查找路径,默认就是sdcard。然后就是把文件都塞到一个ArrayAdapter,作为ListView的adapter,这样ListView就能够显示当前路径的所有文件以及文件夹了。

    private  static final String[] targets = {"mp4", "mkv"};
    private String path;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_file);

        path = Environment.getExternalStorageDirectory().getPath();
        if (getIntent().hasExtra("path")) {
            path = getIntent().getStringExtra("path");
        }
        setTitle(path);

        List fileValues = new ArrayList();
        List dirValues = new ArrayList();
        File dir = new File(path);
        if (!dir.canRead()) {
            setTitle(getTitle() + " inaccessable");
        }

        for (File f : dir.listFiles()) {
            String fileName = f.getName();
            if (fileName.startsWith("."))
                continue;
            if (f.isFile()) {
                for (String target : targets) {
                    if (fileName.endsWith(target))
                        fileValues.add(fileName);
                }
            } else if (f.isDirectory())
                dirValues.add(fileName);
        }

        Collections.sort(fileValues);
        Collections.sort(dirValues);
        fileValues.addAll(dirValues);

        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_2, android.R.id.text1, fileValues);
        setListAdapter(adapter);
    }

响应点击事件

重写onListItemClick来响应点击事件,这里假如选择的是一个文件夹,那么就还是调用ListFileActivity来显示文件夹内的文件列表,假如选择的是一个文件,那么就直接调用MainActivity来播放。当然在MainActivity中也要做相应的修改,是他能够接受一个从外部的intent传递过来的path。

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        String fileName = (String)getListAdapter().getItem(position);
        if (path.endsWith(File.separator)) {
            fileName = path + fileName;
        } else {
            fileName = path + File.separator + fileName;
        }

        if (new File(fileName).isDirectory()) {
            Intent intent = new Intent(this, ListFileActivity.class);
            intent.putExtra("path", fileName);
            startActivity(intent);
        } else {
            Toast.makeText(this, fileName + "is Not directory", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra("path", fileName);
            startActivity(intent);
        }
    }