伙计们我有一个问题,当我们在android中编写一个xml时,我的代码给出了一个Exception作为权限被拒绝.任何人都可以告诉它将如何被删除.
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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)File newxmlfile = new File("C:/new.xml");
您正在尝试在驱动器C上创建文件.Android是基于Linux的系统,因此没有驱动器.存储设备可以安装到root("/")或任何其他文件夹.
您的应用程序/data/<pakcage-name>文件夹可用.试着写在那里.此外,您可以尝试写入外部存储,但您的progrma需要获得权限才能执行此操作:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Run Code Online (Sandbox Code Playgroud)
这应该在您的清单文件中提及.
在这里阅读更多.
| 归档时间: |
|
| 查看次数: |
64126 次 |
| 最近记录: |