Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
682 views
in Technique[技术] by (71.8m points)

how to create xml file in android

Guys i have a problem that my code gives an Exception as Permission Denied when we write a xml in android. can any one tell that How it will be removed.

package com.ex.createXml;

import android.app.Activity;
import android.os.Bundle;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.xmlpull.v1.XmlSerializer;
import android.os.Environment;
import android.util.Log;
import android.util.Xml;
import android.widget.TextView;


public class createXml extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        File newxmlfile = new File("/data/new.xml");
        try{
            newxmlfile.createNewFile();
        }catch(IOException e)
        {
            Log.e("IOException", "Exception in create new File(");
        }
        FileOutputStream fileos = null;
        try{
            fileos = new FileOutputStream(newxmlfile);

        }catch(FileNotFoundException e)
        {
            Log.e("FileNotFoundException",e.toString());
        }
        XmlSerializer serializer = Xml.newSerializer();
        try{
        serializer.setOutput(fileos, "UTF-8");
        serializer.startDocument(null, Boolean.valueOf(true));
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        serializer.startTag(null, "root");
        serializer.startTag(null, "Child1");
        serializer.endTag(null, "Child1");
        serializer.startTag(null, "Child2");
        serializer.attribute(null, "attribute", "value");
        serializer.endTag(null, "Child2");
        serializer.startTag(null, "Child3");
        serializer.text("Some text inside child 3");
        serializer.endTag(null,"Child3");
        serializer.endTag(null,"root");
        serializer.endDocument();
        serializer.flush();
        fileos.close();
        //TextView tv = (TextView)findViewById(R.);

        }catch(Exception e)
        {
            Log.e("Exception","Exception occured in wroting");
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
File newxmlfile = new File("C:/new.xml");

You're trying to create your file on drive C. Android is a linux-based system, so there are no drives there. The storage devices can be mounted to root ("/") or any other folder.

For your application /data/<pakcage-name> folder is available. Try to write there. Also, you can try to write to external storage, but your progrma will need a permission to do that:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

This should be mentioned within your manifest file.

Read more here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...