Android的写入SD卡 [英] android writing to sdcard
问题描述
我不能够写入的SD卡。我不能创建一个目录或文件,我缺少什么我在清单中有这样
<使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAGE>< /使用许可权>
而这在我的code
btnSave.setOnClickListener(新View.OnClickListener()
{
公共无效的onClick(查看arg0中)
{
//创建一个目录;所有祖先目录必须存在
布尔成功=(新的文件(/ SD卡/方法/))mkdirs()。
//创建文件
尝试{
myfile文件=新的文件(/ SD卡/方法/方法1 Square.xml);
文件file = MYFILE;
//创建文件,如果它不存在
布尔success1 = file.createNewFile();}
赶上(IOException异常E)
{}
//写入文件
尝试{
BufferedWriter将出=新的BufferedWriter(新的FileWriter(/ SD卡/方法/方法1 Square.xml));
out.write(< XML版本= \\1.0 \\>);
out.write(< MSO的应用程序id = \\Excel.Sheet \\>);
out.write(工作簿的xmlns = \\瓮:架构 - 微软-COM:办公室:■preadsheet \\);
out.close();
} //结束试
赶上(IOException异常E)
{
} //抓到底 } //结束公开 }); //结束保存按钮
第一个事情:
只要勾选成功的变量,它是否真的返回真正
第二件事:
当你已经很难$ C $的CD / SD卡,相反,我建议你使用的目录: Environment.getExternalStorageDirectory()
,因为在某些设备上的SD卡根目录为的/ mnt / SD卡因此上述方法得到的根目录。
第三件事:
您应先检查SD卡是否被安装或没有。
示例:
文件SD卡= Environment.getExternalStorageDirectory();
文件DIR =新的文件(sdcard.getAbsolutePath()+/目录/子目录); //你的情况,只需要给/方法dir.mkdirs();
档案文件=新的文件(目录,文件名); //你的情况,只是给方法1 Square.xmlFileOutputStream中F =新的FileOutputStream(文件);
I am not able to write to the SDCARD. I can not create a Directory or a File, what am I missing I have this in the manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
And this in my code
btnSave.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0)
{
// Create a directory; all ancestor directories must exist
boolean success = (new File("/sdcard/Methods/")).mkdirs();
//create file
try{
File myFile = new File("/sdcard/Methods/Method 1 Square.xml");
File file = myFile;
// Create file if it does not exist
boolean success1 = file.createNewFile();}
catch (IOException e)
{}
//write to file
try {
BufferedWriter out = new BufferedWriter(new FileWriter("/sdcard/Methods/Method 1 Square.xml"));
out.write ("<?xml version=\"1.0\"?>");
out.write ("<?mso-application progid=\"Excel.Sheet\"?>");
out.write ("Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
out.close();
} // end try
catch (IOException e)
{
} // end catch
} // end public
}); // end SAVE Button
1st Thing:
Just check the 'success' variable, whether it returns really true.
2nd Thing:
As you have hard coded the /sdcard, Instead i suggest you get the directory using: Environment.getExternalStorageDirectory()
because in some devices the sd-card root directory is /mnt/sdcard so the above method to get root directory.
3rd Thing:
You should first check whether the SD-card is mounted or not.
Example:
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdcard.getAbsolutePath() + "/dir/subdir"); // in your case, just give /Methods
dir.mkdirs();
File file = new File(dir, "filename"); // in your case, just give "Method 1 Square.xml"
FileOutputStream f = new FileOutputStream(file);
这篇关于Android的写入SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!