0
在我的應用程序中,我從SD卡中檢索文件,檢查後是.jpg oe .png圖片我在文件數組中存儲文件的路徑。但是我得到NullPointerException也存在文件路徑。以下代碼有什麼問題。請幫助在Android中從sdcard中檢索文件
File[] image;
File f = new File("/sdcard/download");//or Environment.getExternalStorageDirectory().getPath()
File[] files = f.listFiles();
int j=0;
for(int i = 0; i < files.length; i++,j++) {
File file1 = files[i];
String myfile = file1.getPath().substring(file1.getPath().lastIndexOf("/")+1,file1.getPath().length()).toLowerCase();
System.out.println("file is : "+files[i]);
if(myfile.endsWith(".jpeg")||myfile.endsWith(".png")|| myfile.endsWith(".gif") || myfile.endsWith(".jpg"))
{
System.out.println("file is : "+files[i]);
image[j]=files[i];//here i'm getting exception
}
}
者除外
02-15 19:28:36.463: E/AndroidRuntime(12918): FATAL EXCEPTION: main
02-15 19:28:36.463: E/AndroidRuntime(12918): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.zproject/com.my.zproject.ImportFile}: java.lang.NullPointerException
02-15 19:28:36.463: E/AndroidRuntime(12918): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-15 19:28:36.463: E/AndroidRuntime(12918): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-15 19:28:36.463: E/AndroidRuntime(12918): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-15 19:28:36.463: E/AndroidRuntime(12918): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-15 19:28:36.463: E/AndroidRuntime(12918): at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 19:28:36.463: E/AndroidRuntime(12918): at android.os.Looper.loop(Looper.java:123)
02-15 19:28:36.463: E/AndroidRuntime(12918): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-15 19:28:36.463: E/AndroidRuntime(12918): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 19:28:36.463: E/AndroidRuntime(12918): at java.lang.reflect.Method.invoke(Method.java:521)
02-15 19:28:36.463: E/AndroidRuntime(12918): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-15 19:28:36.463: E/AndroidRuntime(12918): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-15 19:28:36.463: E/AndroidRuntime(12918): at dalvik.system.NativeStart.main(Native Method)
02-15 19:28:36.463: E/AndroidRuntime(12918): Caused by: java.lang.NullPointerException
02-15 19:28:36.463: E/AndroidRuntime(12918): at com.my.zproject.ImportFile.onCreate(ImportFile.java:76)
02-15 19:28:36.463: E/AndroidRuntime(12918): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
發佈錯誤日誌 – Nickolaus 2013-02-15 14:09:53
另外,不要硬編碼的SD卡。改爲使用環境類 – 2013-02-15 14:10:18
我發佈了錯誤日誌 – 2013-02-15 14:14:09