所以我的Android Gallery中有3个文件夹。我试图将这些文件夹的文件读入File数组,然后从那里将图像源转换为实际的ImageViews

我的问题是引用文件数组时出现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;
}


就像我说的那样,错误是当我使用list.length引用一个File数组时,例如我在第一个代码段中创建的allTops。

这是LogCat错误消息:

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


我也有我的同意:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


很抱歉投入了这么多代码,但我觉得有人在这里弄清楚这个问题是必要且足够的。

我现在已经花了几个小时,以为我会问社区!非常感谢大家。

编辑:感谢所有的反馈!这是我的代码,用于保存拍摄的图像。这应该显示所有已保存图像的路径。另外,我的LogCat说文件路径为/ 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;
}

最佳答案

根据File.listFiles()的文档,如果null不是目录,它将返回File

关于为什么会这样,有三种(非常相似的)替代方案...


由(例如)... String返回的Environment.getExternalStorageDirectory().getPath() + "/DressIt Tops/"代表无效目录(..."/DressIt Bottoms/"和/或..."/DressIt Shoes/"可能相同)。
返回的Strings是有效路径,但其中一个或另一个是文件,而不是目录。
目录确实存在,但是您输入的目录名称有错字-记住文件系统区分大小写。如果您将“ / DressIt Tops /”目录创建为“” / dressIt tops /”(例如),则找不到有效路径。


还有一件事-永远不要假设任何路径到Android文件系统上的任何地方-记录Environment.getExternalStorageDirectory().getPath()实际返回的内容,检查它是否是您创建目录的位置,并检查所有文件的大小写是否正确。

09-27 20:05