安卓:写在SD卡一个XML [英] Android: Write a xml on SD-Card

查看:110
本文介绍了安卓:写在SD卡一个XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我的Andr​​oid的应用程序有问题。
我想保存数据列表中的 XML 并保存为 SD卡

I have a problem with my android-app. I want to save a Data-list in a xml and save it to SD-Card.

在这里,code片段从我的活动:

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);
            }
        });

这是我的解析器:

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_STORAG​​E ...但我不能在我的存储写!我得到了以下的logcat消息:

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?

谢谢!

推荐答案

这是一个在黑暗中拍摄 - 它的可能的是,你有你的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:

File myXml= new File(path+"new.xml");

这样:

File myXml= new File(path+"/new.xml");

这篇关于安卓:写在SD卡一个XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆