【问题标题】:how to create xml file in android如何在android中创建xml文件
【发布时间】:2011-03-03 13:26:21
【问题描述】:

伙计们,我有一个问题,当我们在 android 中编写 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.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");
        }
    }
}

【问题讨论】:

  • 请不要再问同样的问题。

标签: android


【解决方案1】:
File newxmlfile = new File("C:/new.xml");

您正试图在驱动器 C 上创建文件。Android 是基于 linux 的系统,因此那里没有驱动器。存储设备可以挂载到根目录(“/”)或任何其他文件夹。

对于您的应用程序,/data/<pakcage-name> 文件夹可用。试着写在那里。此外,您可以尝试写入外部存储,但您的程序需要权限才能执行此操作:

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

这应该在您的清单文件中提及。

阅读更多here

【讨论】:

  • 感谢 Vladimir,您确实认为它给出了 Permission denied Exception。那么,你能告诉它如何删除吗?
【解决方案2】:

您似乎试图在 c:/ 的位置创建一个文件,这不是 android 系统上的有效路径标识符。 Android 来自 linux 环境。

http://developer.android.com/guide/topics/data/data-storage.html点击此链接了解更多关于安卓数据存储的信息

如果您想在本地电脑上创建文件,您首先需要在您的 Android 设备上创建它,然后从设备(模拟器或您的真实手机)中提取它

【讨论】:

    【解决方案3】:

    你不能写信到那个位置。

    File newxmlfile = new File("/data/new.xml");
    

    你可以把它保存到internal storage, or cache.

    【讨论】:

      【解决方案4】:

      由于 Android 安全模型,您不能写信给 /data/new.xml。那是试图访问根文件系统,这就是您被拒绝许可的原因。试试不带前导斜线。

      FileOutputStream fos = openFileOutput("new.xml", MODE_PRIVATE);
      fos.write(...);
      

      这应该与您的应用相关。

      【讨论】:

      • 但是文件存储在哪里?
      • @SanatPandey 嘿,这个文件存储在哪里?你找到答案了吗?我不知道文件是在哪里创建的??
      【解决方案5】:

      【讨论】:

      • 我已经设置了manifest权限,但是错误依旧。
      【解决方案6】:

      你可以用这个:

      File xmlDir = new File(Environment.getExternalStorageDirectory().getPath() + "/data/new.xml");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多