Saturday, January 15, 2011

How to write a file to Android filesystem using Adobe AIR

The other night I ended up banging my head against the wall wondering why I couldn't write a file (or even create a directory for that matter) to my Samsung Galaxy S filesystem using a Flex application running on Adobe AIR.


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.


 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.



Tuesday, January 11, 2011

SSL Certificate or Code Signing Certificate for ClickOnce Deployment

I am using ClickOnce deployment to deploy an Excel Com Addin and was wondering if I could sign the installer with an SSL Certificate and kill two birds with one stone.


The short answer is NO, they are different!


So what is the difference between an SSL and Code Signing certificate?
It seems that the only difference is the value of the purpose field in the certificate. The issuer of the certificate chooses for what purpose the certificate is created.  Applications that do the signing and verifying check the purpose field of the certificate.
In short:
  • An SSL Certificate is used for authentication of your website/application server.
  • A Code Signing Certificate is used for integrity/authentication of the exe,dll,... you deliver.

Here is a great little thread I found resulting from someone asking the same question as me

How to Control Alt Delete (Ctrl Alt Delete) on Remote Desktop

To perform a  "Control+Alt+Delete" on Remote Desktop press Control+Alt+End (Ctrl+Alt+End)

It's one of those things that also seems to escape my mind, so I am finally putting it up.

Short and sweet.  What a great first blog!