android 列出根目录下面所有文件

该博客展示了如何在Android应用中列出根目录(/)下的所有文件。通过创建List并使用File对象,遍历目录,将文件名和路径分别存储到items和paths列表中。点击列表项时,会检查文件是否可读,如果是目录则进入该目录,否则显示文件信息。
摘要由CSDN通过智能技术生成


//items 存放显示的名称  paths存放文件路径 rootPath起始目录
 private List<String> items = null;
 private List<String> paths = null;
 private String rootPath = "/";
 private TextView mPath;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mPath = (TextView) findViewById(R.id.textView1);
  getFileDir(rootPath);
 }

 private void getFileDir(String filePath) {

  mPath.setText(filePath);
  items = new ArrayList<String>();
  paths = new ArrayList<String>();
  File files = new File(filePath);
  File [] f = files.listFiles();
  if(!filePath.equals(rootPath)){
   //第一个设置为回到根目录
   items.add("back to"+rootPath);
   paths.add(rootPath);

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值