如何列出目录中的文件? [英] how to list files in a directory?

查看:127
本文介绍了如何列出目录中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯新Android开发,所以请原谅我,如果这是一个容易的事。

我想获得的所有文件在SD卡上的目录,并在旋转显示出来,但我只是不能工作如何。
这就是我,我甚至不知道是否有任何这是任何好处。

  //如果它不存在创建此目录?
文件SD =新的文件(/ SD卡/ MyFolder文件);                 //获取的文件列表
                 文件[] = sdDirList sd.listFiles();                 //它们添加到微调阵列(这使它崩溃)
                 的for(int i = 0; I< sdDirList.length;我++)
                 array_spinnerLoad [I] = sdDirList [I] .getName();

所以我在哪里去了?
任何有用的链接到一个易于在任何地方使用教程?
它不必须是一个微调,只是一些名单,我可以从

选择

感谢您:)

编辑:
它不崩溃与此,但微调数组不填充

 文件[] = sdDirList sd.listFiles();
             如果(sdDirList!= NULL)
             {
                 array_spinnerLoad =新的String [sdDirList.length]             的for(int i = 0; I< sdDirList.length;我++)             array_spinnerLoad [I] = sdDirList [I] .getName();
         }


解决方案

如果 / SD卡/ MyFolder中不存在或不是一个目录,那么 listFiles 收益。你可能用NPE崩溃。您可以使用存在()的isdirectory()来诊断什么是错的。 (另外,你可以测试 sdDirList!= NULL

P.S。这将帮助你获得更多准确的答案张贴关于崩溃的详细信息 - 例外,如

im new to android development so please forgive me if this is an easy thing to do.

i want to get all the files in a directory on the sd card and display them in a spinner, but i just cant work out how. this is what i have and i dont even know if any of it is any good.

//creates this directory if its not there??     
File sd = new File("/sdcard/myfolder");

                 //gets a list of the files
                 File[] sdDirList = sd.listFiles(); 

                 //add them to the spinner array (this makes it crash)
                 for(int i=0;i<sdDirList.length;i++) 
                 array_spinnerLoad[i] = sdDirList[i].getName();

so where am i going wrong? any usefull link to an easy to use tutorial anywhere? it doesnt have to be a spinner, just some list i can select from

thank you :)

edit: it doesnt crash with this but the spinner array is not filled

File[] sdDirList = sd.listFiles(); 
             if (sdDirList != null)
             {
                 array_spinnerLoad=new String[sdDirList.length];

             for(int i=0;i<sdDirList.length;i++)

             array_spinnerLoad[i] = sdDirList[i].getName();
         }

解决方案

If /sdcard/myfolder doesn't exist or is not a directory, then listFiles returns null. You're probably crashing with a NPE. You can use exists() and isDirectory() to diagnose what's wrong. (Plus, you can test that sdDirList != null.)

P.S. It would help you to get more accurate answers to post details about the crash -- such as the exception.

这篇关于如何列出目录中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆