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

【Android】如何实现一个简单的文件浏览器_第1张图片
【Android】如何实现一个简单的文件浏览器_第2张图片

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文件权限(Linux的权限)
  2. Android和IOS录制mp3语音文件的方法
  3. Android中多层Fragment嵌套,调用相册返回Uri无法显示图片的问题解
  4. android读取SDCard任意路径下的文件
  5. Android WebView图片显示问题
  6. android 缩放图片与内存溢出
  7. android image cache 图片缓存 异步 下载
  8. android如何获取SD卡上的多媒体文件
  9. android studio中.9.png图片处理

随机推荐

  1. Android(安卓)studio报错module not spec
  2. Android下Xml解析技术(四)、pull生成Xml文
  3. get方式和post方式的请求
  4. Android(安卓)ActionBar与ViewPager合用
  5. 较深入的分析Content Providers用法
  6. Android(安卓)ListView元素间隙线自定义
  7. Android传感器的介绍
  8. Android(安卓)实时视频采集—Cameara预览
  9. Android消息推送(广播机制)+通知
  10. android手记之--广播接收者