阅读背景:

Android文件管理器FileManager

来源:互联网 

读取存储空间下的文件并以ListView的情势显示出来。


package com.zms.filemanager;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
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;


public class Main extends Activity implements AdapterView.OnItemClickListener,
        AdapterView.OnItemLongClickListener {

    private ListView fileView;
    private String path = "/sdcard";// 文件路径
    private List<Map<String, Object>> items;
    private SimpleAdapter adapter;
    private File backFile = null;
    private String currentPath = "/sdcard";
    private boolean flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setTitle("文件管理");
        fileView = (ListView) findViewById(R.id.filelist);
        listDir(path);
    }

    /**
     * 动态绑定文件信息到listview上
     *
     * @param path
     */
    private void listDir(String path) {
        items = bindList(path);

        // if(items!=null){
        adapter = new SimpleAdapter(this, items, R.layout.file_row,
                new String[]{"name", "path", "img"}, new int[]{R.id.name,
                R.id.desc, R.id.img});
        fileView.setAdapter(adapter);
        fileView.setOnItemClickListener(this);
        fileView.setOnItemLongClickListener(this);
        fileView.setSelection(0);
        // }
    }

    /**
     * 返回所有文件目录信息
     *
     * @param path
     * @return
     */
    private List<Map<String, Object>> bindList(String path) {
        File[] files = new File(path).listFiles();
        // if(files!=null){
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(
                files.length);
        Map<String, Object> root = new HashMap<String, Object>();
        root.put("name", "/sdcard");
        root.put("img", R.drawable.folder);
        root.put("path", "根目录");
        list.add(root);
        Map<String, Object> pmap = new HashMap<String, Object>();
        pmap.put("name", "返回");
        pmap.put("img", R.drawable.back);
        pmap.put("path", "上级目录");
        list.add(pmap);
        for (File file : files) {
            Map<String, Object> map = new HashMap<String, Object>();
            if (file.isDirectory()) {
                map.put("img", R.drawable.folder);
            } else {
                map.put("img", R.drawable.doc);
            }
            map.put("name", file.getName());
            map.put("path", file.getPath());
            list.add(map);
        }
        return list;
        /*
         * } return null;
		 */
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                            long arg3) {

        if (position == 0) {// 返回到/sdcard目录
            path = "/sdcard";
            listDir(path);
        } else if (position == 1) {// 返回上一级目录
            toParent();
        } else {
            if (items != null) {
                path = (String) items.get(position).get("path");
                File file = new File(path);
                if (file.canRead() && file.canExecute() && file.isDirectory()) {
                    listDir(path);
                } else {
                    openFile(file);
                    Toast.makeText(this, "嘿嘿", Toast.LENGTH_SHORT).show();
                }
            }
        }
        backFile = new File(path);
    }

    private void toParent() {// 回到父目录
        File file = new File(path);
        File parent = file.getParentFile();
        if (parent == null) {
            listDir(path);
        } else {
            path = parent.getAbsolutePath();
            listDir(path);
        }
    }

    /**
     * 文件操作提醒
     *
     * @param id
     */
    private void myNewDialog(int id) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        switch (id) {
            case 0:
                LayoutInflater factory = LayoutInflater.from(this);
                final View textEntryView = factory.inflate(R.layout.filedialog,
                        null);
                builder.setTitle("创立文件夹");
                builder.setView(textEntryView);
                builder.setPositiveButton("肯定", new CreateDialog(textEntryView));
                builder.setNegativeButton("撤消", null);
                break;
            case 1:
                LayoutInflater factory2 = LayoutInflater.from(this);
                final View textEntryView2 = factory2.inflate(R.layout.filedialog,
                        null);
                builder.setTitle("重命名文件");
                builder.setView(textEntryView2);
                builder.setPositiveButton("肯定", new RenameDialog(textEntryView2));
                // <span
                //>builder.setNegativeButton("撤消",
                // null);</span>
                builder.setNegativeButton("撤消", null);
                break;
            case 2:
                builder.setTitle("肯定要删除吗?");
                builder.setPositiveButton("肯定", new DeleteDialog());
                builder.setNegativeButton("撤消", null);
                break;
        }
        builder.create().show();
    }

    private void mySortDialog(int id) { // 排序

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        switch (id) {
            case 0:// 按名称排序
                LayoutInflater factory = LayoutInflater.from(this);
                final View textEntryView = factory.inflate(R.layout.filedialog,
                        null);
                builder.setTitle("创立文件夹");
                builder.setView(textEntryView);
                builder.setPositiveButton("肯定", new CreateDialog(textEntryView));
                builder.setNegativeButton("撤消", null);
                break;
            case 1:// 按时光排序
                LayoutInflater factory2 = LayoutInflater.from(this);
                final View textEntryView2 = factory2.inflate(R.layout.filedialog,
                        null);
                builder.setTitle("重命名文件");
                builder.setView(textEntryView2);
                builder.setPositiveButton("肯定", new RenameDialog(textEntryView2));
                // <span
                //>builder.setNegativeButton("撤消",
                // null);</span>
                builder.setNegativeButton("撤消", null);
                break;
            case 2:
                builder.setTitle("肯定要删除吗?");
                builder.setPositiveButton("肯定", new DeleteDialog());
                builder.setNegativeButton("撤消", null);
                break;
        }
        builder.create().show();
    }

    /**
     * 依据路径删除指定的目录或文件,不管存在与否
     *
     * @param sPath 要删除的目录或文件
     * @return 删除胜利返回 true,否则返回 false。
     */
    public boolean DeleteFolder(String sPath) {
        flag = false;
        File file = new File(sPath);
        // 断定目录或文件是不是存在
        if (!file.exists()) { // 不存在返回 false
            return flag;
        } else {
            // 断定是不是为文件
            if (file.isFile()) { // 为文件时调用删除文件办法
                return deleteFile(sPath);
            } else { // 为目录时调用删除目录办法
                return deleteDirectory(sPath);
            }
        }
    }

    /**
     * 删除单个文件
     *
     * @param sPath 被删除文件的文件名
     * @return 单个文件删除胜利返回true,否则返回false
     */
    public boolean deleteFile(String sPath) {
        flag = false;
        File file = new File(sPath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }

    /**
     * 删除目录(文件夹)和目录下的文件
     *
     * @param sPath 被删除目录的文件路径
     * @return 目录删除胜利返回true,否则返回false
     */
    public boolean deleteDirectory(String sPath) {
        // 如果sPath不以文件分隔符结尾,主动添加文件分隔符
        if (!sPath.endsWith(File.separator)) {
            sPath = sPath + File.separator;
        }
        File dirFile = new File(sPath);
        // 如果dir对应的文件不存在,或不是一个目录,则退出
        if (!dirFile.exists() || !dirFile.isDirectory()) {
            return false;
        }
        flag = true;
        // 删除文件夹下的所有文件(包含子目录)
        File[] files = dirFile.listFiles();
        for (int i = 0; i < files.length; i++) {
            // 删除子文件
            if (files[i].isFile()) {
                flag = deleteFile(files[i].getAbsolutePath());
                if (!flag)
                    break;
            } // 删除子目录
            else {
                flag = deleteDirectory(files[i].getAbsolutePath());
                if (!flag)
                    break;
            }
        }
        if (!flag)
            return false;
        // 删除当前目录
        if (dirFile.delete()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 文件刷新
     *
     * @param file
     */
    private void fileScan(String file) {
        Uri data = Uri.parse("file://" + file);

        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data));
    }

    /**
     * 启动文件打开
     *
     * @param f
     */
    private void openFile(File f) {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(android.content.Intent.ACTION_VIEW);

        // 获得文件媒体类型
        String type = getMIMEType(f);
        if (type == null)
            return;
        intent.setDataAndType(Uri.fromFile(f), type);
        startActivity(intent);
    }

    // add for test start
    private final String[][] MIME_MapTable = {
            // {后缀名, MIME类型}
            {".bin", "application/octet-stream"}, {".bmp", "image/bmp"},
          package com.zms.fil




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: