Environment.getExternalStorageDirectory()在Android 4.1中不起作用

时间:2014-03-22 10:32:15

标签: android filepath android-sdcard

这有点奇怪,我无法在我的SD卡上获得文件的完整路径。控制台显示未找到任何文件或目录。这是我到目前为止所做的尝试:

File file1=new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "/CDAInfo.txt");
    fileArray.add(file1);

logcat的:

 03-22 15:54:10.459: E/AndroidRuntime(17542): FATAL EXCEPTION: main
03-22 15:54:10.459: E/AndroidRuntime(17542): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vinit.airdrive/com.vinit.airdrive.MainActivity}: java.lang.NullPointerException
03-22 15:54:10.459: E/AndroidRuntime(17542):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2063)
03-22 15:54:10.459: E/AndroidRuntime(17542):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2088

请帮忙吗?

3 个答案:

答案 0 :(得分:1)

试试这个

    File file1= new File(Environment.getExternalStorageDirectory().getPath(),"CDAInfo.txt");

答案 1 :(得分:1)

我发现问题,你的手机没有外部存储,所以这样做

    String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
//External storage available
            mExternalStorageAvailable = mExternalStorageWriteable = true;
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            mExternalStorageAvailable = true;
            mExternalStorageWriteable = false;
//only internal available
        } else {
            mExternalStorageAvailable = mExternalStorageWriteable = false;
// no one is available
        }

请阅读this

我希望它有所帮助

答案 2 :(得分:0)

请得到这样的路径,

  File file1=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/CDAInfo.txt");