从sdcard读取idata时出现Android NullPointerException

时间:2014-10-25 09:30:22

标签: android

在我的代码中,我已成功将一些图像存储到 SDCARD 。现在,我尝试从 SDCARD 中读取这些图片,但我在尝试阅读文件时收到NullPointerException

这是我的代码:

// list of available files in  path
ArrayList<String> f = new ArrayList<String>();   
File[] listFile;

String ExternalStorageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath();

String targetPath = ExternalStorageDirectoryPath + "/saved_images";

Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show();
File targetDirector = new File(targetPath);

File[] files = targetDirector.listFiles();
for (File file : files){          
    // This line shows error
    imageAdapter.add(file.getAbsolutePath());
} 

1 个答案:

答案 0 :(得分:0)

你应该添加两张支票。

if ( !targetDirector.exists())
   return; and display toast

if (files==null)
   return; and display toast