Android:在 SD 卡上写入 xml
我的 Android 应用程序有问题。 我想将数据列表保存在 xml 中,并将其保存到 SD 卡。
这是我的活动中的代码片段:
Button btnActivity = (Button)findViewById(R.id.modul_save);
btnActivity.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Handlerlist templist=new Handlerlist(); //default-constructor for handlerlist
Parser_Writer temp_parser=new Parser_Writer(); //default-constructor for praser(writing)
templist.add_modul(name, abk, 1, 0, "Hallo", room, 1, 2, 3, 1.3, true); //save input-data in the list
temp_parser.write_xml(templist); //calls the parser(writing)
Intent in = new Intent(modul_create.this,modul_overview.class);
startActivity(in);
}
});
这是我的解析器:
public class Parser_Writer {
public Parser_Writer(){
}
public void write_xml(Handlerlist h){
XStream xstream = new XStream(new DomDriver());
try {
String xml = xstream.toXML(h);
String path=Environment.getExternalStorageDirectory().toString();
File myXml= new File(path+"new.xml");
myXml.createNewFile();
FileWriter fileIO =new FileWriter(myXml);
fileIO.write("<?xml version='1.0' encoding='utf-8'?>");
fileIO.write(xml.toString());
fileIO.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我还添加了 android.permission.WRITE_EXTERNAL_STORAGE
...但我无法在我的存储上写入!我收到以下 logcat 消息:
java.io.IOException: Read-only file system
at java.io.File.createNewFileImpl(Native Method)
at java.io.File.createNewFile(File.java:1160)
at Parser.Parser_Writer.write_xml(Parser_Writer.java:38)
at StundentenApp.main.modul_create$1.onClick(modul_create.java:51)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
有人可以帮助我吗?
谢谢!
I have a problem with my android-app.
I want to save a Data-list in a xml and save it to SD-Card.
Here the code fragment from my activity:
Button btnActivity = (Button)findViewById(R.id.modul_save);
btnActivity.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Handlerlist templist=new Handlerlist(); //default-constructor for handlerlist
Parser_Writer temp_parser=new Parser_Writer(); //default-constructor for praser(writing)
templist.add_modul(name, abk, 1, 0, "Hallo", room, 1, 2, 3, 1.3, true); //save input-data in the list
temp_parser.write_xml(templist); //calls the parser(writing)
Intent in = new Intent(modul_create.this,modul_overview.class);
startActivity(in);
}
});
This is my parser:
public class Parser_Writer {
public Parser_Writer(){
}
public void write_xml(Handlerlist h){
XStream xstream = new XStream(new DomDriver());
try {
String xml = xstream.toXML(h);
String path=Environment.getExternalStorageDirectory().toString();
File myXml= new File(path+"new.xml");
myXml.createNewFile();
FileWriter fileIO =new FileWriter(myXml);
fileIO.write("<?xml version='1.0' encoding='utf-8'?>");
fileIO.write(xml.toString());
fileIO.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I also add the android.permission.WRITE_EXTERNAL_STORAGE
... but i can not write on my storage!! I got the following logcat-message:
java.io.IOException: Read-only file system
at java.io.File.createNewFileImpl(Native Method)
at java.io.File.createNewFile(File.java:1160)
at Parser.Parser_Writer.write_xml(Parser_Writer.java:38)
at StundentenApp.main.modul_create$1.onClick(modul_create.java:51)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Someone here who could help me?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个盲目的尝试 - 可能毕竟你的 SD 卡被锁定了 - 但请尝试将以下内容更改
为:
This is a shot in the dark -- it might be that you have your SD card locked after all -- but try changing the following:
to this: