//看来看一下效果

Android 获取文件目录以及文件的删除 .

//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;@Overrideprotected 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();        }      }  }

摘自:http://blog.csdn.net/yanglian20009/article/details/7087724

更多相关文章

  1. android 使用http协议上传文件
  2. android Pull方式解析xml文件
  3. android 获取SD卡的图片及其路径
  4. android Http文件上传
  5. Android Studio生成APK文件名带上版本号等信息
  6. android文件管理器(1)
  7. 在android中获取文件的MD5值
  8. android获取assert资源文件

随机推荐

  1. Android(安卓)根据屏幕尺寸适配控件尺寸(
  2. ANR(1)---理解Android(安卓)ANR的信息收集
  3. Android(安卓)获取未读短信(sms)数量
  4. Android(安卓)Studio报错 程序包android.
  5. Android(安卓)启动模式
  6. android中scrollview与webview冲突事件
  7. Android本地相册图片URI转换绝对路径
  8. Android(安卓)Parcel学习
  9. android view获取在屏幕上的绝对坐标
  10. android 自定义控件的style