//看来看一下效果
//main.xml文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView android:layout_width="fill_parent"
- android:id="@+id/TextView"
- android:background="#ffff00"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <ListView android:id="@android:id/list"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </ListView>
- </LinearLayout>
//文件清单里面要加权限, 不然无法删除文件
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
//核心代码
- package sn.len.service;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.AlertDialog;
- import android.app.ListActivity;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- public class TestServiceActivity extends ListActivity
- {
- private List<String> items = null;//存放名称
- private List<String> paths = null;//存放路径
- private String rootPath = "/";
- private TextView tv;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- tv = (TextView) this.findViewById(R.id.TextView);
- this.getFileDir(rootPath);//获取rootPath目录下的文件.
- }
- public void getFileDir(String filePath)
- {
- try
- {
- this.tv.setText("当前路径:"+filePath);// 设置当前所在路径
- items = new ArrayList<String>();
- paths = new ArrayList<String>();
- File f = new File(filePath);
- File[] files = f.listFiles();// 列出所有文件
- // 如果不是根目录,则列出返回根目录和上一目录选项
- if (!filePath.equals(rootPath))
- {
- items.add("返回根目录");
- paths.add(rootPath);
- items.add("返回上一层目录");
- paths.add(f.getParent());
- }
- // 将所有文件存入list中
- if(files != null)
- {
- int count = files.length;// 文件个数
- for (int i = 0; i < count; i++)
- {
- File file = files[i];
- items.add(file.getName());
- paths.add(file.getPath());
- }
- }
- //可以去查一相这个类
- //this 上下文
- //android.R.layout.simple_list_item_1 是Android显示列表每一项自己的主题
- //item则就是根据你自己的内容来显示
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, items);
- this.setListAdapter(adapter);
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- }
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id)
- {
- super.onListItemClick(l, v, position, id);
- String path = paths.get(position);
- final File file = new File(path);
- //如果是文件夹就继续分解
- if(file.isDirectory())
- {
- this.getFileDir(path);
- }
- else
- {
- AlertDialog.Builder alertDialog=new AlertDialog.Builder(this);
- alertDialog.setTitle("提示");
- alertDialog.setMessage(file.getName()+" 是一个文件,你要删除这个文件吗");
- //设置左面确定
- alertDialog.setPositiveButton
- ("确定", new DialogInterface.OnClickListener()
- {
- public void onClick(DialogInterface dialog, int which)
- {
- //执行删除,或者什么。。。操作
- File delFile=new File(file.getAbsolutePath());
- if(delFile.exists())
- {
- Log.i("PATH",delFile.getAbsolutePath());
- delFile.delete();
- //刷新界面
- getFileDir(file.getParent());
- }
- }
- }
- );
- //设置右边取消
- alertDialog.setNegativeButton
- ("取消", new DialogInterface.OnClickListener()
- {
- public void onClick(DialogInterface dialog, int which)
- {
- //执行操作
- getFileDir(file.getParent());
- }
- }
- );
- alertDialog.show();
- }
- }
- }