使用ListView实现了一个最简单的SD卡文件浏览器。


Manifest文件

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.loushuai.simpleplayer">    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity" android:label="@string/title_simple_palyer">            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            intent-filter>        activity>    application>manifest>

layout文件

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.loushuai.simpleplayer.MainActivity">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Hello World!" />RelativeLayout>

list_item.xlm

<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/myView1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:textAppearance="?android:attr/textAppearanceLarge"    android:gravity="center_vertical"    android:paddingLeft="5dip"    android:singleLine="true">TextView>

MainActivity.java

package com.example.loushuai.simpleplayer;import android.app.ListActivity;import android.os.Bundle;import android.os.Environment;import android.util.Log;import android.view.View;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Stack;public class MainActivity extends ListActivity {    public static final String COLUMN_NAME_NAME = "name";    private SimpleAdapter adapter = null;    private List> itemList;    private Stack pathHistory = null;    private String curPath;    String[] from = { COLUMN_NAME_NAME } ;    int[] to = {R.id.myView1};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        pathHistory = new Stack();        String sDStateString = Environment.getExternalStorageState();        if(sDStateString.equals(Environment.MEDIA_MOUNTED)) {            File SDFile = Environment.getExternalStorageDirectory();            curPath = SDFile.getAbsolutePath() + "/";            itemList = getData(curPath);            adapter = new SimpleAdapter(this, itemList, R.layout.list_item, from, to);            setListAdapter(adapter);        }    }    private List> getData(String path) {        List> list = new ArrayList>();        Map map = new HashMap();        map.put(COLUMN_NAME_NAME, "..");        list.add(map);        File file = new File(path);        if (file.listFiles().length > 0) {            for (File f : file.listFiles()) {                map = new HashMap();                String name = f.getName();                if (f.isDirectory()) {                    name += "/";                }                map.put(COLUMN_NAME_NAME, name);                list.add(map);                Log.d("Scan", " path " + path + f.getName());            }        }        return list;    }    @Override    protected void onListItemClick(ListView l, View v, int position, long id) {        super.onListItemClick(l, v, position, id);        String path;        if (position > 0) {            pathHistory.push(curPath);            path = curPath + itemList.get(position).get(COLUMN_NAME_NAME);        } else { // uplevel            if (!pathHistory.empty())                path = pathHistory.pop();            else // root                path = curPath;        }        Log.d("List View Click", " position: " + position + " name: " + path);        File file = new File(path);        if (file.isDirectory()) {            updateList(path);            curPath = path;        } else {            Toast toast = Toast.makeText(getApplicationContext(), itemList.get(position).get(COLUMN_NAME_NAME) + " is a file", Toast.LENGTH_SHORT);            toast.show();        }    }    private void updateList(String path) {        itemList.clear();        itemList = getData(path);        adapter = new SimpleAdapter(this, itemList, R.layout.list_item, from, to);        setListAdapter(adapter);        adapter.notifyDataSetChanged();    }}

完整代码请移步:https://github.com/loushuai/SimpleFileViewer

更多相关文章

  1. android点击按钮发出声音
  2. android ADB PUSH 安装apk的具体步骤 及 与 ADB INSTALL 的区别
  3. android读取SDCard任意路径下的文件
  4. Android文件权限(Linux的权限)
  5. android 打开word pdf excle 进行预览 (工具类总结一)
  6. Android(安卓)Studio环境搭建
  7. Android(安卓)Studio报错:the minSdk version should not be decl
  8. Android叠加更新
  9. NPM 和webpack 的基础使用

随机推荐

  1. Android底部fragment互相跳转
  2. Android录音--AudioRecord、MediaRecorde
  3. 小白用网格布局实现简单的计算器 ——And
  4. Android(安卓)Studio配置百度地图开发环
  5. Ubuntu编译ijkplayer支持https和更多视频
  6. Android(安卓)注解
  7. cocos2d-x android 添加新场景报错: unde
  8. ExpandableListView研究
  9. Android去除url参数中的特殊字符
  10. 安卓四大组件之——ContentProvider学习