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: