The code was simple enough and apparently the code should have worked on my mobile without any additional changes. However, when things didn't work I was left stumped.
The code:
private function saveFile() : void {
var myFile:File = File.documentsDirectory.resolvePath("Test Folder/test.txt");
var fs:FileStream = new FileStream();
fs.open(myFile,FileMode.WRITE);
fs.writeUTFBytes("blah blah");
fs.close();
}
Using the code above nothing was written to the filesystem. WTF?? I tried to figure out the paths of the special directories to see if was trying to write the file in a weird directory somewhere.
The debug code below:
debugText += File.applicationDirectory.nativePath + "\n";
debugText += File.applicationStorageDirectory.nativePath + "\n";
debugText += File.documentsDirectory.nativePath + "\n";
debugText += File.userDirectory.nativePath + "\n";
resulted in the following output: /data/data/air.TestApp.debug/TestApp.debug/Local Store
/mnt/sdcard
/mnt/sdcard
/
So it should have correctly written to the internal SD card on my Galaxy S but there wasn't a file or directory in sight. The next thing I tried to do was create a directory using the code below, while checking for any errors.
This resulted in "error #3001" which means "File or directory access denied."
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/runtimeErrors.html
Why was access being denied since the "/mnt/sdcard/" folder had read and write access? The answer lied in the security permissions for an android application as described on the android developer guide.
http://developer.android.com/guide/topics/security/security.html
By default, an android application doesn't have permission to write to external storage so in order to write to my Samsung Galaxy S's internal SD card ("/mnt/sdcard/") I needed to set the WRITE_EXTERNAL_STORAGE permission to my applications XML descriptor file ("TestApp-app.xml").
After adding this line everything worked like a charm. Such relief.
var file:File = File.documentsDirectory.resolvePath("Test Folder");
try {
file.createDirectory();
} catch (e:Error) {
debugText += e.message;
}
This resulted in "error #3001" which means "File or directory access denied."
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/runtimeErrors.html
Why was access being denied since the "/mnt/sdcard/" folder had read and write access? The answer lied in the security permissions for an android application as described on the android developer guide.
http://developer.android.com/guide/topics/security/security.html
By default, an android application doesn't have permission to write to external storage so in order to write to my Samsung Galaxy S's internal SD card ("/mnt/sdcard/") I needed to set the WRITE_EXTERNAL_STORAGE permission to my applications XML descriptor file ("TestApp-app.xml").
<android>
<manifestAdditions><![CDATA[
<manifest>
<!-- See the Adobe AIR documentation for more information about setting Google Android permissions -->
...
...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
...
...
</manifest>
]]></manifestAdditions>
</android>
After adding this line everything worked like a charm. Such relief.