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

//看来看一下效果


//main.xml文件

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView android:layout_width="fill_parent"    
  8.          android:id="@+id/TextView"    
  9.          android:background="#ffff00"    
  10.          android:layout_height="wrap_content"  
  11.          android:text="@string/hello"  
  12.           />    
  13.      <ListView android:id="@android:id/list"    
  14.          android:layout_width="fill_parent"    
  15.          android:layout_height="wrap_content">    
  16.      </ListView>  
  17.   
  18. </LinearLayout>  

//文件清单里面要加权限, 不然无法删除文件

[html]  view plain copy
  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
  2. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>   

//核心代码

[java]  view plain copy
  1. package sn.len.service;  
  2.   
  3. import java.io.File;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6. import android.app.AlertDialog;  
  7. import android.app.ListActivity;  
  8. import android.content.DialogInterface;  
  9. import android.os.Bundle;  
  10. import android.util.Log;  
  11. import android.view.View;  
  12. import android.widget.ArrayAdapter;  
  13. import android.widget.ListView;  
  14. import android.widget.TextView;  
  15.   
  16. public class TestServiceActivity extends ListActivity   
  17. {  
  18.     private List<String> items = null;//存放名称    
  19.     private List<String> paths = null;//存放路径    
  20.     private String rootPath = "/";    
  21.     private TextView tv;  
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState)   
  24.     {  
  25.         super.onCreate(savedInstanceState);    
  26.         setContentView(R.layout.main);    
  27.         tv = (TextView) this.findViewById(R.id.TextView);    
  28.         this.getFileDir(rootPath);//获取rootPath目录下的文件.   
  29.     }   
  30.     public void getFileDir(String filePath)   
  31.     {    
  32.         try  
  33.         {    
  34.             this.tv.setText("当前路径:"+filePath);// 设置当前所在路径    
  35.             items = new ArrayList<String>();    
  36.             paths = new ArrayList<String>();    
  37.             File f = new File(filePath);    
  38.             File[] files = f.listFiles();// 列出所有文件    
  39.             // 如果不是根目录,则列出返回根目录和上一目录选项    
  40.             if (!filePath.equals(rootPath))   
  41.             {    
  42.                 items.add("返回根目录");    
  43.                 paths.add(rootPath);    
  44.                 items.add("返回上一层目录");    
  45.                 paths.add(f.getParent());    
  46.             }    
  47.             // 将所有文件存入list中    
  48.             if(files != null)  
  49.             {    
  50.                 int count = files.length;// 文件个数    
  51.                 for (int i = 0; i < count; i++)   
  52.                 {    
  53.                     File file = files[i];    
  54.                     items.add(file.getName());    
  55.                     paths.add(file.getPath());    
  56.                 }    
  57.             }    
  58.     
  59.             //可以去查一相这个类  
  60.             //this 上下文  
  61.             //android.R.layout.simple_list_item_1 是Android显示列表每一项自己的主题  
  62.             //item则就是根据你自己的内容来显示  
  63.             ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, items);    
  64.             this.setListAdapter(adapter);    
  65.         }  
  66.         catch(Exception ex)  
  67.         {    
  68.             ex.printStackTrace();    
  69.         }    
  70.     
  71.     }    
  72.       
  73.     @Override    
  74.     protected void onListItemClick(ListView l, View v, int position, long id)   
  75.     {    
  76.         super.onListItemClick(l, v, position, id);    
  77.         String path = paths.get(position);    
  78.         final File file = new File(path);    
  79.         //如果是文件夹就继续分解    
  80.         if(file.isDirectory())  
  81.         {    
  82.             this.getFileDir(path);    
  83.         }  
  84.         else  
  85.         {    
  86.               
  87.             AlertDialog.Builder alertDialog=new AlertDialog.Builder(this);  
  88.             alertDialog.setTitle("提示");  
  89.             alertDialog.setMessage(file.getName()+" 是一个文件,你要删除这个文件吗");  
  90.             //设置左面确定  
  91.             alertDialog.setPositiveButton  
  92.             ("确定"new DialogInterface.OnClickListener()  
  93.                 {    
  94.                     public void onClick(DialogInterface dialog, int which)   
  95.                     {    
  96.                              //执行删除,或者什么。。。操作  
  97.                             File delFile=new File(file.getAbsolutePath());  
  98.                             if(delFile.exists())  
  99.                             {  
  100.                                 Log.i("PATH",delFile.getAbsolutePath());  
  101.                                 delFile.delete();  
  102.                                 //刷新界面  
  103.                                 getFileDir(file.getParent());     
  104.                             }  
  105.                     }    
  106.                 }  
  107.             );  
  108.             //设置右边取消  
  109.             alertDialog.setNegativeButton  
  110.             ("取消"new DialogInterface.OnClickListener()  
  111.                 {    
  112.                     public void onClick(DialogInterface dialog, int which)   
  113.                     {    
  114.                             //执行操作  
  115.                         getFileDir(file.getParent());  
  116.                     }    
  117.                 }  
  118.             );  
  119.             alertDialog.show();  
  120.         }    
  121.     }    
  122. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值