简体   繁体   English

在Android中创建xml文件时出错

[英]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. 我有以下代码创建一个xml文件并将其保存在设备中。

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). 错误之一是/sdcard/Downloads/new.xml(没有这样的文件或目录)。 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. 您必须确保根sdcard存在。 Try to use Environment.getExternalStorageDirectory() to get external directory instead of static path /sdcard. 尝试使用Environment.getExternalStorageDirectory()来获取外部目录,而不是静态路径/ sdcard。 If you want to use static path please print Environment.getExternalStorageDirectory() to see what is the root of your external storage 如果要使用静态路径,请打印Environment.getExternalStorageDirectory()以查看外部存储的根目录是什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我遇到错误,显示通过 android.view.InflateException 对 class 片段进行膨胀时出错:二进制 XML 文件 - i am having error that shows Error inflating class fragment by android.view.InflateException: Binary XML file 在 Android 上启动 Appium 时出现错误 - I am getting an error when I am launching Appium on Android 在Android中创建VCF文件时出错 - Error when creating a vcf file in android 当我在Android Java文件中使用Fragment,Tabspager时出现错误 - I am getting error when i use Fragment,Tabspager in Android java file 在JHipster中创建Spring Bean时为什么会出错? - Why am I getting an error when creating Spring Beans in JHipster? 从解析的XML文件创建表时出现Android错误 - Android error while creating table from parsed XML file 错误:androidmanifest.xml文件丢失 - >我缺少什么? - error: androidmanifest.xml file missing --> What am i missing? 为什么我在添加 spring-boot-starter-security 依赖项时在 pom.xml 文件中出现未知错误? - Why I am getting an unknown error in pom.xml file when I add the spring-boot-starter-security dependency? Android创建和编写xml到文件 - Android creating and writing xml to file 下载pdf格式的文件时出现Xml解析异常 - I am having an Xml parse Exception when i am downloading a file in pdf format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM