I'm using an ActionScript Library project to share code and assets / resources between a Mobile and a Desktop ActionScript projects.
The library project has been added to the two other projects via the 'Add Project' option on the 'Library Path' tab, with the linkage type 'Merged into Code', and all the classes within it can be accessed by the other projects, and work properly.
However it contains a SQLite database file, which I want to copy out to the File.applicationStorageDirectory on the target system on the first load of the app, and I'm not sure how to get a reference to the file within the library project to copy it out.
The location of the db file is: LibraryProj - src/database/dbFile.db and I thought using File.applicationDirectory and then a path 'into' the swf would give me access to it, but none of the following tests say the file exists.
var test:File;
test = File.applicationDirectory.resolvePath("app:/src/database/dbFileDb.db");
trace("test.exists==" + test.exists);
test = File.applicationDirectory.resolvePath("src/database/dbFileDb.db");
trace("test.exists==" + test.exists);
test = File.applicationDirectory.resolvePath("database/dbFileDb.db");
trace("test.exists==" + test.exists);
test = File.applicationDirectory.resolvePath("dbFileDb.db");
trace("test.exists==" + test.exists);
Is this the correct method to copy resource / asset files out of a swf containing merged libraries and onto the app's storage directory? Is it even possible to share resources / assets from Library projects in this way?
Any advice would be very much appreciated.
After doing some more research about using the [Embed] tag in AS3, I've now worked out that this is what I should have been using to make the file available to consuming projects (I'd previously only used it for images, and didn't think of it for other file types too).
[Embed('igniteDb.db', mimeType="application/octet-stream")]
public static const myReferenceDbFile:Class;
To copy the file to the File.applicationStorageDirectory i'm using the following code. It converts the embedded file to a byte array, and then writes this out via a FileStream class to the destination file.
//write the embedded database file data into app user files directory
var bundleDbBytes:ByteArray;
bundleDbBytes = new myReferenceDbFile();//gets a reference to the embedded db file
var outputDbFile:File = File.applicationStorageDirectory.resolvePath(DB_FILE_NAME);
var fileStream:FileStream = new FileStream();
fileStream.open(outputDbFile, FileMode.UPDATE);
fileStream.writeBytes(bundleDbBytes);
fileStream.close();
And hey presto, the database is ready to use.
Related
Using ActionScript 3 I want to access multiple text files from a subfolder within my assets folder: project/src/assets/myFiles/
I know how to embed and read one specific text file if I know the exact file name:
[Embed(source="assets/myFiles/file1", mimeType="application/octet-stream")]
private static const FILE1:Class;
private function read():void {
var text:String = (new FILE1() as ByteArray).toString();
}
Since I have multiple files in the folder myFiles I'd like to read all of them.
I don't know the exact number and names of these files at compile time.
Is it possible to access them by retreiving an array of objects? Or is it possible to list all the file names read them otherwise?
Since I don't know the names at compile time I don't know how to embed and access those files.
Example of my files:
project/src/assets/myFiles/file1
project/src/assets/myFiles/file2
project/src/assets/myFiles/anotherFile
Edit: I am using AIR.
Edit: I am using Flex.
Edit: Current approach is to not use the asset directory but a dedicated (further) directory. I include this folder in the package so it and its content is automatically created in the application directory on compililation / installation. I read this folder via FileStream as suggested within the comments by Organis.
I'm running my program on AIR. I want my game to save the high score to a text file so it can be stored when the program is closed. I've tried using filestreams, however I've found that the application directory is read only. Is there a better way to do this?
The AIR application storage directory is designed for saving these sorts of preferences and user settings:
For every AIR application, there is a unique associated path that
defines the application storage directory. This directory is unique to
each application and user. You can use this directory to store
user-specific, application-specific data (such as user data or
preferences files).
Access it through the File class:
var file:File = File.applicationStorageDirectory;
file = file.resolvePath("prefs.xml");
See also: Reading from and writing to an XML preferences file
The only caveat I have found with using this directory is that it does not get removed when the AIR app is uninstalled using the .air installer/uninstaller.
I am trying to save a string into .txt file using action script 3 ,I use this code :
var fileRef:FileReference;
fileRef = new FileReference();
fileRef.save("sasa", "saveFile.txt");
But this code asks the user the path location but i don't want to do it ,i need to save in the current directory of swf file without asking path from user?
Any ideas will be appreciated
I am so new in AS3
Best regards
I'm afraid you can't do that for security reasons. But if you're doign an AIR app you can use FileStream class
I have an application prepared in Action Script 3. There is a part related to requesting a file from application storage directory. However when I use following codes (xmlLoader part), program looks into app directory.
How can I make it look into application storage directory or may any other code alternatives be used for this request? Thanks.
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest(Puzzleoriginalpath + ".xml"));
You can get the storage directory through
File.applicationStorageDirectory.nativePath
Then build your path:
File.applicationStorageDirectory.nativePath + "/examplepuzzle.xml"
In a very simple first AIR application (I'm using Flash Builder 4.5), I am trying to accomplish the following on my MacBook:
Read a local file (JSON format) into the AIR app.
Parse through the file.
Display some selected contents in a grid.
That's it.
I've found an example that does some simple JSON parsing in Flex, but the problem is that it loads the JSON source from a remote web site.
So do I need to load any file-specific libraries to make this work in AIR? Or can I simply refer to the file by using the Mac file-path? I just want the local file to be the JSON source. The parsing is already taken care of.
You can use FileStream like:
var myFile:File = File.documentsDirectory.resolvePath("AIR Test/test.txt");
var myFileStream:FileStream = new FileStream();
myFileStream.open(myFile, FileMode.READ);
var yourJSONdata:String = myFileStream.readUTFBytes(myFileStream.bytesAvailable);
myFileStream.close();
Initializing a FileStream object, and opening and closing files