我是 Android 的初级开发人员,在尝试访问 SD 卡上的 XML 文件时遇到了以下问题。
首先,我执行了以下检查: - android manifest 中给出了读/写外部存储的权限 - 文件存在于指定位置 - 我检查了:fileexists = true; 可以读=真;isfile = true - 外部存储状态 = 已安装
FileInputStream 仍然给我 FileNoteFoundException。编码:
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
//probeersel
public void myClickHandler(View view) {
switch (view.getId()) {
case R.id.button1:
XPath xpath = XPathFactory.newInstance().newXPath();
File mealsource = new File(getExternalFilesDir(null)
+ "/test.xml");
Toast.makeText(this, "External Files Dir: " + getExternalFilesDir(null), Toast.LENGTH_LONG).show();
Toast.makeText(this, "Exists: " + String.valueOf(mealsource.exists()), Toast.LENGTH_LONG).show();
Toast.makeText(this, "Can read: " + String.valueOf(mealsource.canRead()), Toast.LENGTH_LONG).show();
Toast.makeText(this, "Is file: " + String.valueOf(mealsource.isFile()), Toast.LENGTH_LONG).show();
Toast.makeText(this, "External Storage State: " + Environment.getExternalStorageState(), Toast.LENGTH_LONG).show();
Toast.makeText(this, "Absolute path " + mealsource.getAbsolutePath(), Toast.LENGTH_LONG).show();
FileInputStream mealstream = new FileInputStream(mealsource);
InputSource inputsource = new InputSource(mealstream);
expression = "Meals/Meal/ShrtDesc/text()";
meallist = (NodeList) xpath.evaluate(expression, inputsource, XPathConstants.NODE);
etc
回顾一下:我所有的检查都表明该文件可以读取并准备好执行操作,但 FileInputStream 构造函数似乎对此视而不见。我是什么做的?我错过了什么?它可能与xml的内容有关吗?
非常感谢您能给我的任何帮助或指点。