3

I am trying to read all files and folders in a directory specified by the user.

I use:

File file = new File("/");
String[] files = file.list();

This works and gives me a list of the file names and folders in the root directory.
This works fine, but when I specify anything other then "/" it crashes.

How do I fix this, am I not giving it a valid path or is there something wrong with my code?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
gumba
  • 72
  • 1
  • 7
  • 1
    Post the stack trace from the crash. – sddamico Jan 04 '14 at 18:32
  • what do you mean by anything other than `/` ? Also before getting the list please check if the file exists. – Rohan Kandwal Jan 04 '14 at 18:36
  • Sorry but I have never gotten an emulator to work so I run the program straight on my phone and it doesn't give any crash report. – gumba Jan 04 '14 at 18:39
  • I'm not specifying a file or atleast I shouldn't as far as i understand. I have tried other paths like "/storage", "storage/", "/storage/", etc as many as I could think of(storage – gumba Jan 04 '14 at 18:42
  • 1
    "and it doesn't give any crash report." - It does. Look at LogCat in Eclipse. – Aron Lorincz Jan 04 '14 at 18:49
  • I am not running it on an emulator I run it on my phone. If you mean that a crash report is stored on the phone and that you can look at it from eclipse, how? – gumba Jan 04 '14 at 18:53
  • use DDMS, you will see your phone there and also your logcat from phone. – Rohan Kandwal Jan 04 '14 at 18:59
  • Also in DDMS you can see your file structure, so you can understand which folder exists and which don't. – Rohan Kandwal Jan 04 '14 at 18:59
  • 1
    @gumba: The Logcat tab in your IDE should show the errors you've encountered. If not (which is unlikely), you can view error log using an app called [CatLog](https://play.google.com/store/apps/details?id=com.nolanlawson.logcat&hl=en) available from Google Play. Install this free app into your device and post your app's error log from Catlog back here. Without error log, we can't help you. – ChuongPham Jan 04 '14 at 19:06

1 Answers1

1

/ is the root of android and there exists not folder name storage, so when you are using

String[] files = file.list();

you are asking android to give the list of files inside storage which doesn't even exists. Thus, you are getting errors.

Best practice is to assume that the folder might not exist, even if you are sure it will eg user can delete a folder that you created on the SDCard which will cause crashes in your apps. So use the following code

if(file.exists()){
String [] filenames = file.list();
}

And this is just my guess but i think you are searching for SDCard, if so then use

Environment.getExternalStorageDirectory()

with permission to read SDCard files.

Update :- For reading files in internal and external memory please see the following link https://stackoverflow.com/a/17546843/1979347

Community
  • 1
  • 1
Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107
  • I just chose storage for testing and I know it exists on my phone. Now I am just trying to read all files and folder somewhere other then "/". – gumba Jan 04 '14 at 19:07
  • Where exactly is **storage**, I think you are referring path incorrectly. – Rohan Kandwal Jan 04 '14 at 19:10
  • If I read from "/" it is in there, if i specify "/" it works as it should but anything other just crash. I think it should be just /storage but that doesn't work. – gumba Jan 04 '14 at 19:14
  • I really don't think that **storage** will be in `/`. Please use my `if ` loop to check if the file exists since `/storage` is correct. You can't make a directory in the root folder unless you have root permissions and have used a root explorer app to make the folder. If you have both, then the folder can be there else not. – Rohan Kandwal Jan 04 '14 at 19:20
  • I dont recal giving any permission but I have an app on which I have seen that storage exists. Do you know the permission code? – gumba Jan 04 '14 at 19:24
  • You can' get root access using permission. Google it you will see. Since you don't know about root I am 100% confident that **/storage** isn't in your root path. Is **storage** in your SDCard ? – Rohan Kandwal Jan 04 '14 at 19:43
  • When I use the app to see the files ans folders I go back as far as I can, there I see storage. If I use "/" I get to the same place. I dont know what it is stored on but in storage are one folder called "sdcard0" and one called "usbdisk" which I think are sdcard and the internal storage but I am not sure. – gumba Jan 04 '14 at 19:53
  • I just want to ask you one question. Is it a new phone or you bought it from someone? If it is new, and you never rooted it then there is no way that it is rooted. Some of the folders on `/` are acct, cache, config, data, etc. Do you see anyone of those? – Rohan Kandwal Jan 05 '14 at 05:26
  • It is a new phone and I have not rooted it. And yes I do see acct, cache, config, etc. If i use the app I see those and when I use / on my app I see those. My app works when I enter / but when I try to add some of the folders which I can see when I enter / it crashes. – gumba Jan 05 '14 at 12:57
  • I tried some other files in / and they work but some still doesn't, for examples /system works but /storage doesn't??? – gumba Jan 05 '14 at 13:58
  • If you can see the directories of some apps but not of storage then I really don't know what's wrong. I think you should mark this question solved and ask a new question. – Rohan Kandwal Jan 05 '14 at 15:29