gpt4 book ai didi

java - Android 无法找到已放入 SD 卡的文件目录

转载 作者:行者123 更新时间:2023-11-29 08:05:40 24 4
gpt4 key购买 nike

问题是我通过 DDMS Perspective 将文件放在数据和/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";

对我没有任何作用。

最佳答案

使用

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

代替

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


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

getExternalStorageDirectory () :

Gets the Android external storage directory. This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. You can determine its current state with getExternalStorageState().

getExternalStorageState() :

Gets the current state of the primary "external" storage device.

关于java - Android 无法找到已放入 SD 卡的文件目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11433542/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com