1

问题是我通过 DDMS Perspective 将文件放在 data 和 /data/data 文件夹中,当我尝试访问该文件时,会发生 FileNotFoundException。代码:

                String path = Environment.getExternalStorageState()+"/analyzer_settings.xml";
                System.out.println(path);

                try {
                    Scanner in = new Scanner(new File(path));
                    while (in.hasNext()) {
                        System.out.println(in.nextLine());
                    }
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

打印例外:

07-11 17:10:02.470: I/System.out(13506): mounted/analyzer_settings.xml
07-11 17:10:02.500: W/System.err(13506): java.io.FileNotFoundException: /mounted/analyzer_settings.xml: open failed: ENOENT (No such file or directory)

我也试过:

String path = Environment.getExternalStorageState()+"/data/analyzer_settings.xml";
or 
String path = Environment.getExternalStorageState()+"/data/data/analyzer_settings.xml";

没有什么对我有用。

4

2 回答 2

4

采用

String path = Environment.getExternalStorageDirectory().getAbsolutePath()
              +"analyzer_settings.xml";

代替

 String path = Environment.getExternalStorageState()+"/analyzer_settings.xml";


编辑: 因为正如文档所说:

获取外部存储目录()

获取 Android 外部存储目录。如果该目录已被用户安装在他们的计算机上、已从设备中删除或发生了其他问题,则该目录当前可能无法访问。您可以使用 getExternalStorageState() 确定其当前状态。

获取外部存储状态()

获取主要“外部”存储设备的当前状态。

于 2012-07-11T13:19:23.953 回答
1

试试这个编码,

       File sdcard = Environment.getExternalStorageDirectory();

    File file = new File(sdcard,"billdump1.xml");//Don't use '/' symbols
于 2012-07-11T13:22:47.240 回答