简体   繁体   中英

Error when I am creating xml file in Android

I have the following code that creates an xml file and saves it in the device.

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.activity_create_xml);

  File newxmlfile = new File("/sdcard/Downloads/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");
  }
 }
}

When I am running the app I get these errors

06-02 15:20:03.753 2530-2530/com.ex.createXml E/IOException:        Exception in create new File(
06-02 15:20:03.753 2530-2530/com.ex.createXml   E/FileNotFoundException: java.io.FileNotFoundException:    /sdcard/Downloads/new.xml (No such file or directory)
06-02 15:20:03.753 2530-2530/com.ex.createXml E/Exception: Exception    occured in wroting

I have add these permissions to the manifest

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

One of the errors is /sdcard/Downloads/new.xml (No such file or directory). Can you give me a proper path to save my file or any idea if something else is wrong ? thank you

You have to make sure the root sdcard was exist. Try to use Environment.getExternalStorageDirectory() to get external directory instead of static path /sdcard. If you want to use static path please print Environment.getExternalStorageDirectory() to see what is the root of your external storage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM