Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [31]

By Root 2568 0
that an attribute exists, do the following:

if (so.data.animal == undefined) {

trace("What are you?");

} else {

trace("Hello again, ", so.data.animal);

}

To display all the attributes, use:

for (var i:String in so.data) {

trace("prop", i, ": ", so.data[i]);

}

// prop food : Grains,Avocado,Carrot

// prop isVegetarian : true

// prop stuff : [object, Object]

// prop animal : Hamster

To check the size of the shared object, use:

so.size;

// 140

If you wish to remove an attribute, use the delete command. Setting the variable to undefined or null changes its value but doesn’t remove it:

delete so.data.animal;

If you wish to remove all of the attributes, use the clear() method. This method will also delete the shared object itself:

so.clear();

The Filesystem


The filesystem should be familiar to anyone who owns a computer. It is responsible for the hierarchical organization of files and projects, with the option to read, write, move, and delete files. In AIR for Android, files can only be created and manipulated in the application storage directory or on the SD card.

The File class belongs to the flash.filesystem package and inherits from the FileReference class. It represents the path to a directory or a file, regardless of whether the directory or file exists or has not yet been created.

You can create a File object to the path of the file you want to access, here the application storage area. The resolvePath function creates the path from the current location which is the .swf file that is currently running:

import flash.filesystem.File;

var file:File

= File.applicationStorageDirectory.resolvePath("hello.txt");

Data is read or written using FileStream. Create it, open it, and close it after the operation is complete.

The FileStream method you should use depends on the type of data you are encoding. For text files, use readUTFBytes and writeUTFBytes. For a ByteArray, use readBytes and writeBytes, and so on. To get a complete list of methods, refer to http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/FileStream.html.

Writing data to the file


Use the FileMode.WRITE method to write data to the file. In the following example, we are creating a folder if one does not already exist, and then we are creating a file in the folder:

import flash.filesystem.File;

import flash.filesystem.FileStream;

import flash.filesystem.FileMode;

var folder:File

= File.applicationStorageDirectory.resolvePath("greetings");

if (!folder.exists) {

folder.createDirectory();

}

var file:File = folder.resolvePath("hello.txt");

Note that although a reference to the new file has been created, the file will not exist until data is saved in it. Let’s write a simple string:

var fileStream:FileStream = new FileStream();

fileStream.open(file, FileMode.WRITE);

fileStream.writeUTFBytes("Can you hear me?");

fileStream.close();

NOTE

To make your file unique, save the data as part of the name as follows:

var now:Date = new Date();

var name:String = "file" +

now.getDay() + "_" + now.getHours() + "_" +

now.getMinutes() + "_" + now.getSeconds();

Reading a file


Use the FileMode.READ method to read a file. In the following example, if the file doesn’t exist, we don’t need to read its data:

var file:File

= File.applicationStorageDirectory.resolvePath("greetings/hello.txt");

if (!file.exists) {

return;

}

var fileStream:FileStream = new FileStream();

fileStream.open(file, FileMode.READ);

var string:String = fileStream.readUTFBytes(fileStream.bytesAvailable);

fileStream.close();

trace(string); // Can you hear me?

Deleting a file


Let’s delete the file first:

var file:File

= File.applicationStorageDirectory.resolvePath("greetings/hello.txt");

if (file.exists) {

file.deleteFile();

}

Now let’s delete the directory. Note the boolean passed in the deleteDirectory function. This is to prevent errors. If the boolean is set to true, the folder is deleted only if the directory exists:

var folder:File

= File.applicationStorageDirectory.resolvePath("greetings");

folder.deleteDirectory(true);

Return Main Page Previous Page Next Page

®Online Book Reader