Android - 文件写入问题
我在将 xml 文件写入 Android 设备 (GalaxyTab 10.1V) 时遇到一些问题。编写 xml
的代码工作正常。我可以在 Eclipse 的 DDMS 透视图中看到写入的文件,并且可以将该文件保存到我的计算机上的任何位置。但是当我用windows资源管理器打开与Eclipse中打开的相同目录时,该文件似乎不存在!该文件在 DDMS 透视图中仍然可见,但 Windows 资源管理器不想(或不能?)向我显示该文件。重新启动平板电脑后,该文件也会显示在 Windows 资源管理器中。
我已经尝试了几种方法来解决这个问题...
编写 .txt 文件 --> 结果相同 -->
尝试了
getExternalFilesDir()
(自 API 级别 8 起推荐)和getExternalStoragePublicDirectory()
而不是getExternalStorageDirectory()
--> ;相同的结果在 USB 调试模式下写入、在“正常”USB 模式下写入、在没有 USB 连接的情况下写入 -->相同的结果
当然我已经在清单文件中设置了在设备上读写的权限
android.permission.WRITE_EXTERNAL_STORAGE android.permission.READ_EXTERNAL_STORAGE
- “显示隐藏文件”在 Windows 文件夹选项中激活,
这就是我的代码的样子...
public boolean write(Result result) {
Serializer serial = new Persister();
File file = new File(Environment.getExternalStorageDirectory()
+ "/Test/result.xml");
try {
serial.write(result, file);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
我使用 SimpleXML 库用于读取和写入我的 xml 文件,但如前所述,在写入普通 .txt 文件时会出现同样的问题
有人知道这种行为的原因是什么吗?
I have some problems when writing an xml file onto my android device (GalaxyTab 10.1V). The code for writing the xml
works fine. I can see the written file at the DDMS
perspective in Eclipse and can save the file to any location on my computer. But when I open the same directory as opened in Eclipse with the windows explorer, it seems that the file does not exist! The file is still visible at the DDMS
perspective, but windows explorer does not want (or can´t?) show me this file. After rebooting the tablet, the file is also shown at the windows explorer.
I have tried several things for solving this problem...
writing a .txt file --> same result
tried
getExternalFilesDir()
(as recommended since API Level 8) andgetExternalStoragePublicDirectory()
instead ofgetExternalStorageDirectory()
--> same resultwriting at USB-Debug mode, writing at "normal" USB mode, writing without USB connection --> same result
of course I have set the permission to read and write on the device at the manifest file
android.permission.WRITE_EXTERNAL_STORAGE
android.permission.READ_EXTERNAL_STORAGE
- "show hidden files" is activated at the windows folder options
this is how my code looks like...
public boolean write(Result result) {
Serializer serial = new Persister();
File file = new File(Environment.getExternalStorageDirectory()
+ "/Test/result.xml");
try {
serial.write(result, file);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
I use the SimpleXML library for reading and writing my xml files but as mentioned before, the same problem occurs when writing a normal .txt file
Does anyone have an idea what the reason for this behaviour could be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论