Reputation: 191
So I have 3 folders in my Android Gallery. I am trying to read the files of these folders into File arrays and then, from there, translate the image source into actual ImageViews
.
My problem is that I'm getting NullPointerException
when referencing my File array. Here is some code:
// 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();
Using these image File arrays...
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);
My CreateList method. The error is when I reference 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;
}
Like I said, the error is when I use list.length to reference one of the File arrays, such as allTops that I created in the first code segment.
And here are the LogCat error messages:
05-05 19:43:49.409: D/dalvikvm(537): Not late-enabling CheckJNI (already on)
05-05 19:43:51.069: D/dalvikvm(537): GC_FOR_ALLOC freed 70K, 3% free 10185K/10439K, paused 63ms
05-05 19:43:51.079: I/dalvikvm-heap(537): Grow heap (frag case) to 10.566MB for 562516-byte allocation
05-05 19:43:51.139: D/dalvikvm(537): GC_CONCURRENT freed 1K, 3% free 10733K/11015K, paused 6ms+7ms
05-05 19:43:51.278: D/com.example.dressit(537): /mnt/sdcard/DressIt Tops
05-05 19:43:51.289: D/AndroidRuntime(537): Shutting down VM
05-05 19:43:51.289: W/dalvikvm(537): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
05-05 19:43:51.319: E/AndroidRuntime(537): FATAL EXCEPTION: main
05-05 19:43:51.319: E/AndroidRuntime(537): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.dressit/com.example.dressit.MainActivity}: java.lang.NullPointerException
05-05 19:43:51.319: E/AndroidRuntime(537): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
05-05 19:43:51.319: E/AndroidRuntime(537): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
05-05 19:43:51.319: E/AndroidRuntime(537): at android.app.ActivityThread.access$600(ActivityThread.java:122)
05-05 19:43:51.319: E/AndroidRuntime(537): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
05-05 19:43:51.319: E/AndroidRuntime(537): at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 19:43:51.319: E/AndroidRuntime(537): at android.os.Looper.loop(Looper.java:137)
05-05 19:43:51.319: E/AndroidRuntime(537): at android.app.ActivityThread.main(ActivityThread.java:4340)
05-05 19:43:51.319: E/AndroidRuntime(537): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 19:43:51.319: E/AndroidRuntime(537): at java.lang.reflect.Method.invoke(Method.java:511)
05-05 19:43:51.319: E/AndroidRuntime(537): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-05 19:43:51.319: E/AndroidRuntime(537): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-05 19:43:51.319: E/AndroidRuntime(537): at dalvik.system.NativeStart.main(Native Method)
05-05 19:43:51.319: E/AndroidRuntime(537): Caused by: java.lang.NullPointerException
05-05 19:43:51.319: E/AndroidRuntime(537): at com.example.dressit.MainActivity.createList(MainActivity.java:125)
05-05 19:43:51.319: E/AndroidRuntime(537): at com.example.dressit.MainActivity.onCreate(MainActivity.java:103)
05-05 19:43:51.319: E/AndroidRuntime(537): at android.app.Activity.performCreate(Activity.java:4465)
05-05 19:43:51.319: E/AndroidRuntime(537): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-05 19:43:51.319: E/AndroidRuntime(537): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
05-05 19:43:51.319: E/AndroidRuntime(537): ... 11 more
05-05 19:43:51.509: D/dalvikvm(537): GC_CONCURRENT freed 420K, 5% free 10747K/11271K, paused 6ms+5ms
05-05 19:44:25.629: I/Process(537): Sending signal. PID: 537 SIG: 9
I do have my permission in place, as well:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Sorry to throw in so much code, but I feel like it is necessary and sufficient for someone to figure out the issue here.
I've spent a few hours on it now and figured I'd ask the community! Thanks so much everyone.
EDIT: Thanks for all of the feedback! Here is my code that saves images taken. This should show you all the path of the saved images. Also, my LogCat says that the file path is /mnt/sdcard/DressIt Tops.
/** 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;
}
Upvotes: 2
Views: 5592
Reputation: 48871
According to the documentation for File.listFiles(), it will return null
if the File
is not a directory.
There are three (very similar) alternatives as to why this my be...
String
returned by (for example)... Environment.getExternalStorageDirectory().getPath() + "/DressIt Tops/"
doesn't represent a valid directory (the same may apply for ..."/DressIt Bottoms/"
and/or ..."/DressIt Shoes/"
).Strings
returned ARE valid paths BUT one or other of them is a file and NOT a directory.One more thing - never assume any path to anywhere on the Android filesystem - log what Environment.getExternalStorageDirectory().getPath()
actually returns, check it's where you created the directories and also check you have everything in the correct case.
Upvotes: 1