11

How to put\save files into your application directory? (adobe air) (code example, please)

Rella
  • 65,003
  • 109
  • 363
  • 636

7 Answers7

17

It's not recomended but it is possible. Construct your File reference like this:

var pathToFile:String = File.applicationDirectory.resolvePath('file.txt').nativePath;
var someFile:File = new File(pathToFile);
Miroslav
  • 194
  • 4
  • 7
  • I don't know by who is not recommended but probably is the only way to create a settings file that gets deleted with the app. – Cristi Băluță Oct 24 '11 at 15:56
  • In a release build, this code will FAIL if you try to write! ADL will allow you to write files in the application directory, but not the the release verson of AIR. The entire application is checksum'd and checked upon load. – ansiart May 21 '12 at 18:51
  • @ansiart Utilizing this solution, I exported a release build and installed. I ran the app, no problem. I closed the app and ran again expecting the checksum errors with no issues. I confirmed that my copied files still existed in the app directory. ( FB4.6 ) I also checked the air package to confirm i didn't accidently put the files in the release. Is there some option I'm missing that causes this to solution to break. – CrashCodes Jul 10 '12 at 14:11
  • A bit of warning though - "On Android, the nativePath property of a File object pointing to the application directory is an empty string." - from AS3 documentation. – analytik Nov 16 '12 at 12:02
  • Works like a charm, thanks. And to add a very valid scenario when, as a developer, you need to save to the application directory, it happens when you have all resources in a folder within the appDir and you also have a custom packer that creates a single container file with all those resources. The application is able to open either one but for development you would use the folder while on release you would send out the packed container file. It would be a bit more complicated to move around the packed file when you can simply have it created in its place. – Florin Mircea Jul 23 '15 at 20:46
9

You can't write to your AIR app's Application Directory, it's not allowed. You can however write to a folder that your AIR app creates in the user's directory, called the Application Storage Directory. If you need config files and the like, that's probably the best place to put them. See 'applicationDirectory' in the docs link below:

http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/

debu
  • 910
  • 6
  • 11
4

@glendon if you try to save directly to applicationDirectory it will indeed throw an error, but it seems you can move the file in the filesystem. i used the code below after yours:

var sourceFile:File = File.applicationStorageDirectory.resolvePath ("file.txt");
var pathToFile:String = File.applicationDirectory.resolvePath ('file.txt').nativePath;
var destination:File = new File (pathToFile);
sourceFile.moveTo (destination, true);

the reason why you 'shouldnt' use the application folder is because not all users have rights to save files in such folder, while everyone will in applicationStorageDirectory.

Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
joe
  • 1,359
  • 14
  • 19
2

The accepted answer works!

But if I do this instead:

var vFile = File.applicationDirectory.resolvePath('file.txt');
var vStream = new FileStream();
vStream.open(vFile, FileMode.WRITE);
vStream.writeUTFBytes("Hello World");
vStream.close();

It will give SecurityError: fileWriteResource. However, if I use applicationStorageDirectory instead, the above code will work. It'll only NOT work if it's applicationDirectory. Moreover, Adobe's documentation also says that an AIR app cannot write to its applicationDirectory.

Now, I wonder if it's a bug on Adobe's part that they allow writing to the applicationDirectory using the way suggested by the accepted answer.

glendon
  • 403
  • 1
  • 4
  • 15
  • On Mac, I was able to create folder once using applicationDirectory at this path: /Users/administrator/Library/Preferences/Main/Local Store/thumbAppStorageDir but after that I was not able to run the same lines of code again. Wierd. – Ankit Tanna Oct 09 '13 at 08:32
1

try this.

var objFile:File = new File(“file:///”+File.applicationDirectory.resolvePath(strFilePath).nativePath);

the output would be like this…

file:///c:\del\userConf.xml

This will work fine.

Sandip Armal Patil
  • 6,241
  • 21
  • 93
  • 160
1

If you want write file into ApplicationDirectory, right?

Please don't forget for write for nativeprocess via powershell with registry key for your currect adobe application ( example: C:\Program Files (x86)\AirApp\AirApp.exe with RunAsAdmin )

  1. nativeprocess saves new registry file
  2. AirApp will restarts into RunASAdmin
  3. AirApp can be writable possible with file :)

Don't worry!

I know that trick like sometimes application write frist via registry file and calls powershell by writing nativeprocess into registry file into registry structures.

Look like my suggestion from adobe system boards / forum was better than access problem with writing stream with file :)

I hope you because you know my nice trick with nativeprocess via powershell + regedit /s \AirApp.reg and AirApp changes into administratived AirApp than it works fine with Administratived mode :) Than your solution should write and you try - Make sure for your writing process by AirApp.

SourceSkyBoxer
  • 141
  • 1
  • 8
0

this function gives your current air application folder which bypasses the security problem:

function SWFName(): String {
        var swfName: String;
        var mySWF = new File(this.loaderInfo.url).nativePath;
        swfName= this.loaderInfo.loaderURL;
        swfName = swfName.slice(swfName.lastIndexOf("/") + 1); // Extract the filename from the url
        swfName = new URLVariables("path=" + swfName).path; // this is a hack to decode URL-encoded values
        mySWF = mySWF.replace(swfName, "");
        return mySWF;
    }