Getting NullPointer exception with File.listfiles()

前端 未结 1 1677
予麋鹿
予麋鹿 2021-01-22 12:00

So I have 3 folders in my Android Gallery. I am trying to read the files of these folders into File arrays and then, from there, translate the image source into actual Ima

相关标签:
1条回答
  • 2021-01-22 12:00

    According to the documentation for File.listFiles(), it will return null if the File is not a directory.

    There are three (very similar) alternatives as to why this my be...

    1. The String returned by (for example)... Environment.getExternalStorageDirectory().getPath() + "/DressIt Tops/" doesn't represent a valid directory (the same may apply for ..."/DressIt Bottoms/" and/or ..."/DressIt Shoes/").
    2. The Strings returned ARE valid paths BUT one or other of them is a file and NOT a directory.
    3. The directories DO exist BUT you have a typo with the directory name - remember the filesystem is case-sensitive. If you've created the "/DressIt Tops/" directory as ""/dressIt tops/" (for example), it won't be found as a valid path.

    One more thing - never assume any path to anywhere on the Android filesystem - log what Environment.getExternalStorageDirectory().getPath() actually returns, check it's where you created the directories and also check you have everything in the correct case.

    0 讨论(0)
提交回复
热议问题