对不起,暧昧的标题,但我做了以下写一个简单的字符串到一个文件:无法写入SD卡 - canWrite是返回false
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
System.out.println("Can write.");
File def_file = new File(root, "default.txt");
FileWriter fw = new FileWriter(def_file);
BufferedWriter out = new BufferedWriter(fw);
String defbuf = "default";
out.write(defbuf);
out.flush();
out.close();
}
else
System.out.println("Can't write.");
}catch (IOException e) {
e.printStackTrace();
}
但root.canWrite()
似乎返回false每次。我没有运行这个模拟器,我通过USB将我的android Eris插入到我的电脑中,并通过Eclipse运行我的手机上的应用程序。有没有办法让我的应用程序权限,所以这不会发生?
此外,此代码似乎创建文件default.txt
但如果它已经存在,它会忽略创建,只是打开它写或我必须赶上类似FileAlreadyExists
(如果存在这样的例外),如何然后打开它并写入?
感谢任何帮助家伙。
我unmout手机从电脑,但仍然出现错误,我在AndroidManifest.xml文件中添加了** WRITE_EXTERNAL_STORAGE **权限,这要感谢提前。 – twlkyao 2013-12-26 15:07:57