Crash, when getting the number of images stored in the SD

I'm trying to get the number of all images stored in the SD, but I don't know why the App crashes.

Code:

File dir = new File(Environment.getExternalStorageDirectory()
            + "/images");
File[] files = dir.listFiles();
//int numberOfImages=files.length;

Toast.makeText(getBaseContext(), "fdds"+files.length, Toast.LENGTH_SHORT).show();

LogCat:

03-19 11:54:44.425: E/AndroidRuntime(11775): FATAL EXCEPTION: main
03-19 11:54:44.425: E/AndroidRuntime(11775): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidbook.MediaStoreDemo/com.androidbook.MediaStoreDemo.MediaStoreDemoActivity}: java.lang.NullPointerException
03-19 11:54:44.425: E/AndroidRuntime(11775): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
03-19 11:54:44.425: E/AndroidRuntime(11775): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
03-19 11:54:44.425: E/AndroidRuntime(11775): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-19 11:54:44.425: E/AndroidRuntime(11775): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
03-19 11:54:44.425: E/AndroidRuntime(11775): at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 11:54:44.425: E/AndroidRuntime(11775): at android.os.Looper.loop(Looper.java:130)
03-19 11:54:44.425: E/AndroidRuntime(11775): at android.app.ActivityThread.main(ActivityThread.java:3691)
03-19 11:54:44.425: E/AndroidRuntime(11775): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 11:54:44.425: E/AndroidRuntime(11775): at java.lang.reflect.Method.invoke(Method.java:507)
03-19 11:54:44.425: E/AndroidRuntime(11775): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
03-19 11:54:44.425: E/AndroidRuntime(11775): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
03-19 11:54:44.425: E/AndroidRuntime(11775): at dalvik.system.NativeStart.main(Native Method)
03-19 11:54:44.425: E/AndroidRuntime(11775): Caused by: java.lang.NullPointerException
03-19 11:54:44.425: E/AndroidRuntime(11775): at com.androidbook.MediaStoreDemo.MediaStoreDemoActivity.onCreate(MediaStoreDemoActivity.java:31)
03-19 11:54:44.425: E/AndroidRuntime(11775): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-19 11:54:44.425: E/AndroidRuntime(11775): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)


Try,

if(files != null)
{
 Toast.makeText(getBaseContext(), "fdds"+files.length, Toast.LENGTH_SHORT).show();
}

public File[] listFiles ()

Returns an array of files contained in the directory represented by this file. The result is null if this file is not a directory. The paths of the files in the array are absolute if the path of this file is absolute, they are relative otherwise.

So just check /images directory..


Please use the following code:

File dir = new File(Environment.getExternalStorageDirectory()
            + "images");
File[] files = dir.listFiles();
//int numberOfImages=files.length;

Toast.makeText(getBaseContext(), "fdds"+files.length, Toast.LENGTH_SHORT).show();

Now your app should not throw any exception.

You should use image instead of /image

链接地址: http://www.djcxy.com/p/96808.html

上一篇: Android inflater例外FileNotFound

下一篇: 崩溃时,获取存储在SD中的图像数量