从sdcard列出文件和目录时出现NullpointerException

时间:2013-12-26 10:17:27

标签: android file list

我试图在nexus7中列出sdcard中的所有文件,但我得到了nullpointer异常。

以下代码在三星tab2中运行正常,但在nexus7中出现问题。

如果有人有想法,请帮助我。

    ArrayList<String> listFolders = new ArrayList<String>();
    ArrayList<String> listFiles = new ArrayList<String>();
    String globalPath = "";




     public void onClikButton(View v) {
            String path = Environment.getExternalStorageDirectory()+"/";

        getDirectory(path);
    }


    public void getDirectory(String path) {
        //String setPath = path;
        globalPath = path;
        Log.i("getDirectory","globalPath = " + globalPath);

        String[] splitPath = path.split("/");
        int count = splitPath.length;
        Log.i("getDirectory","globalPath count = " + count);
        if(count==3)
        {
            // Root
            btnBack.setVisibility(View.GONE);
        }
        else
        {
            btnBack.setVisibility(View.VISIBLE);
        }

        File directory = new File(globalPath);
        File[] listDirectory =null;

        if(directory.exists()) {
                  listDirectory = directory.listFiles();
       for(int i=0; i<listDirectory.length;i++)
        {
        File getFolder = listDirectory[i];
        if(getFolder.isDirectory())
        {
            listFolders.add(getFolder.getName());
        }
       }
    }
     } 

日志: -

12-26 15:49:53.390: E/(6322): results[0]: 3839
12-26 15:49:53.390: E/(6322): results[1]: 3839
12-26 15:49:53.390: E/(6322): results[2]: 0
12-26 15:49:53.390: E/(6322): results[3]: 0
12-26 15:49:53.590: E/AndroidRuntime(6322): FATAL EXCEPTION: main
12-26 15:49:53.590: E/AndroidRuntime(6322): Process: com.allappsmobile.pdfsdk.app, PID: 6322
12-26 15:49:53.590: E/AndroidRuntime(6322): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.allappsmobile.pdfsdk.app/com.allappsmobile.pdfsdk.app.ActivityDirectory}: java.lang.NullPointerException
12-26 15:49:53.590: E/AndroidRuntime(6322):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-26 15:49:53.590: E/AndroidRuntime(6322):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-26 15:49:53.590: E/AndroidRuntime(6322):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-26 15:49:53.590: E/AndroidRuntime(6322):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-26 15:49:53.590: E/AndroidRuntime(6322):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-26 15:49:53.590: E/AndroidRuntime(6322):     at android.os.Looper.loop(Looper.java:136)
12-26 15:49:53.590: E/AndroidRuntime(6322):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-26 15:49:53.590: E/AndroidRuntime(6322):     at java.lang.reflect.Method.invokeNative(Native Method)
12-26 15:49:53.590: E/AndroidRuntime(6322):     at java.lang.reflect.Method.invoke(Method.java:515)
12-26 15:49:53.590: E/AndroidRuntime(6322):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-26 15:49:53.590: E/AndroidRuntime(6322):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-26 15:49:53.590: E/AndroidRuntime(6322):     at dalvik.system.NativeStart.main(Native Method)
12-26 15:49:53.590: E/AndroidRuntime(6322): Caused by: java.lang.NullPointerException
12-26 15:49:53.590: E/AndroidRuntime(6322):     at com.allappsmobile.pdfsdk.app.ActivityDirectory.getDirectory(ActivityDirectory.java:89)
12-26 15:49:53.590: E/AndroidRuntime(6322):     at com.allappsmobile.pdfsdk.app.ActivityDirectory.onCreate(ActivityDirectory.java:47)
12-26 15:49:53.590: E/AndroidRuntime(6322):     at android.app.Activity.performCreate(Activity.java:5231)
12-26 15:49:53.590: E/AndroidRuntime(6322):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-26 15:49:53.590: E/AndroidRuntime(6322):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-26 15:49:53.590: E/AndroidRuntime(6322):     ... 11 more

提前致谢!!

4 个答案:

答案 0 :(得分:0)

替换

String path = Environment.getExternalStorageDirectory()+"/"; 

Environment.getExternalStorageDirectory().getAbsolutePath()

答案 1 :(得分:0)

用这个

替换你的onClickButton()
public void onClikButton(View v) {
        String path = Environment.getExternalStorageDirectory().getAbsolutePath();

    getDirectory(path);
}

答案 2 :(得分:0)

执行以下操作:

  1. 替换

    String path = Environment.getExternalStorageDirectory()+"/"; 
    

    Environment.getExternalStorageDirectory()
    
  2. 替换

    if(count==3)
    

    if(path.equals(Environment.getExternalStorageDirectory().getAbsolutPath()))
    

答案 3 :(得分:0)

我在堆栈溢出时发现这个...可能有帮助..

似乎是known issue affecting Android USB file access over MTP。在重新启动手机之前,MTP缓存已过期。

解决方法是:

  1. 清除“媒体存储”应用的数据

  2. 使用SDrescan应用强制更新。

  3. 或者完全避免使用USB。问题不会影响其他访问文件的方法。例如,尝试AirDroid传输文件。

    无论如何,一切都是指向 / storage / emulated / legacy 的链接,它本身就是 / mnt / shell / emulated / 0 的链接,其中包含文件