Java 获取File.listfiles()的NullPointer异常
所以我的Android图库中有3个文件夹。我试图将这些文件夹的文件读入文件数组,然后从那里将图像源转换为实际的Java 获取File.listfiles()的NullPointer异常,java,android,nullpointerexception,Java,Android,Nullpointerexception,所以我的Android图库中有3个文件夹。我试图将这些文件夹的文件读入文件数组,然后从那里将图像源转换为实际的imageview 我的问题是,在引用我的文件数组时,我得到了NullPointerException。下面是一些代码: // Get image files paths. private File topsFolder = new File(Environment.getExternalStorageDirectory().getPath() + "/DressIt Tops/");
imageview
我的问题是,在引用我的文件数组时,我得到了NullPointerException
。下面是一些代码:
// Get image files paths.
private File topsFolder = new File(Environment.getExternalStorageDirectory().getPath() + "/DressIt Tops/");
private File bottomsFolder = new File(Environment.getExternalStorageDirectory()
.getPath() + "/DressIt Bottoms/");
private File shoesFolder = new File(Environment.getExternalStorageDirectory()
.getPath() + "/DressIt Shoes/");
private File[] allTops = topsFolder.listFiles();
private File[] allBottoms = bottomsFolder.listFiles();
private File[] allShoes = shoesFolder.listFiles();
使用这些图像文件数组
ImageAdapter topAdapter = new ImageAdapter(createList(allTops));
topList.setAdapter(topAdapter);
ImageAdapter midAdapter = new ImageAdapter(createList(allBottoms));
midList.setAdapter(midAdapter);
ImageAdapter botAdapter = new ImageAdapter(createList(allShoes));
botList.setAdapter(botAdapter);
我的CreateList方法。错误是当我引用list.length时
private List<ImageClass> createList(File[] list) {
List<ImageClass> result = new ArrayList<ImageClass>();
ImageClass img;
Bitmap bmp;
if (list.length > 0) {
for (int i = 0; i < list.length; i++) {
bmp = BitmapFactory.decodeFile(list[i].getAbsolutePath());
img = new ImageClass(bmp);
result.add(img);
}
} else
result.add(new ImageClass(this.getResources().getIdentifier(
"no_imgs_yet", "drawable", this.getPackageName())));
return result;
}
我也得到了我的许可:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
根据的文档,如果
文件
不是目录,它将返回null
有三个(非常相似)的选择,为什么这是我的
字符串
(例如)Environment.getExternalStorageDirectory().getPath()+“/DressIt Tops/”
不代表有效的目录(这可能适用于../DressIt Bottoms/”
和/或../DressIt Shoes/“
)字符串
是有效路径,但其中一个是文件而不是目录还有一件事-永远不要假设Android文件系统上的任何位置有任何路径-记录
Environment.getExternalStorageDirectory().getPath()
实际返回的内容,检查目录的创建位置,并检查所有内容的大小写是否正确。list为null,应表示TopFolder.listFiles()返回null。您确定文件“topsFolder”存在并且是一个目录吗?(对于“bottomsFolder”和“shoesFolder”的问题相同),那么哪一行是MainActivity
的第125行?我猜它是如果(list.length>0)
从createList(…)
方法。在这种情况下,allTopes
、allBottoms
或allShoes
中的一个或另一个为空。@JonasCz:你的建议如何导致NPE?@Squonk是对的。你用空列表调用createList,然后试图访问它的长度。你确定你的3个文件夹存在吗?@Squonk,对不起,我的错,如果列表为empty,我会得到一个'ArrayIndexOutOfBounds异常。删除注释。当我记录什么环境时,它返回了/mnt/sdcard。getExternalStorageDirectory().getPath()返回。我将尝试将其添加到文件路径名中。刚刚测试了这个-不走运。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type) {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
String dirName;
if (imageType == 1) {
dirName = "DressIt Tops";
} else if (imageType == 2) {
dirName = "DressIt Bottoms";
} else
dirName = "DressIt Shoes";
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
dirName);
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(dirName, "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else if (type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "VID_" + timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}