can't get file names from sd card
I've got an app that records audio. i'm trying to get the filenames that are stored on the sd card so i can put them in a listview. i know that it's probably not best practice to save file at the root of the sd card but i have:) (the same level as dcim, sounds ect). i'm getting a nullpointerexception regarding the listFiles() method. here's my code. any ideas on the exception?
File sdCardRoot = Environment.getExternalStorageDirectory();
Log.i("root on sd =", ""+ sdCardRoot.getPath());
for (File f : sdCardRoot.listFiles()) {
if (f.isFile()){
String name = f.getName();
Log.i("arr names******", ""+arr.get(0));
arr.add(name);
}else
{
Log.i("file", " no file");
}
Log.i("arr******", ""+arr.size());
}
02-13 19:16:19.063: INFO/ActivityManager(1903): Starting activity: Intent { cmp=com.tecmark/.SdGetList }
02-13 19:16:19.118: INFO/root on sd =(10308): /sdcard
02-13 19:16:19.123: INFO/file(10308): no file
02-13 19:16:19.128: DEBUG/AndroidRuntime(10308): Shutting down VM
02-13 19:16:19.128: WARN/dalvikvm(10308): threadid=3: thread exiting with uncaught exception (group=0x4001b180)
02-13 19:16:19.128: ERROR/AndroidRuntime(10308): Uncaught handler: thread main exiting due to uncaught exception
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tecmark/com.tecmark.SdGetList}: java.lang.NullPointerException
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at android.app开发者_StackOverflow.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at android.os.Handler.dispatchMessage(Handler.java:99)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at android.os.Looper.loop(Looper.java:123)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at android.app.ActivityThread.main(ActivityThread.java:4363)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at java.lang.reflect.Method.invokeNative(Native Method)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at java.lang.reflect.Method.invoke(Method.java:521)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at dalvik.system.NativeStart.main(Native Method)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): Caused by: java.lang.NullPointerException
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at com.tecmark.SdGetList.onCreate(SdGetList.java:38)
You get a NPE, which is probably here:
Log.i("arr******", ""+arr.size());
Did you initialize that list first?
精彩评论