Page tree
Skip to end of metadata
Go to start of metadata

File Operations in Smartface are managed under SMF.IO namespace. Basically, there is a File object which manages File & Directory operations and a FileStream object for reading-writing file content. Predefined directories are defined (details will be shared below), and some other helper content is created.

File Object

The File object is a reference object which points a file location in the operation system. When the object is created, it requires a path with a constructor. It is not important to have the file or directory to exist in that location, which can be created later.

There is no separation of the file and directory by the object level in JavaScript. There is a single File object which handles both.

The contents of the File object can be found on API Docs.

Create

Below are the sample declarations of the File object:

//Creating File
var f1 = new SMF.IO.File("tmp1.txt");
success = f1.createFile(); 
//If it is true that means file created successfully. 
f1.delete();
//You can simply delete your file like above.
f1.move("string directory");
//Files can be move with code above.
f1.copy("string directory");
//You can simply copy files like above 
 
//Creating Directory
var d1 = new SMF.IO.File("tmp1");
success = d1.createDirectory(); 
//If it is true that means directory created successfully. 
//Creating File
var f2 = new SMF.IO.File(SMF.IO.applicationDataDirectory, "tmp2.txt");
success = f2.createFile(); 
//If it is true that means file created successfully. 
f2.delete();
// You can simply delete your file like above.
f2.move("string directory");
//Files can be move with code above.
f2.copy("string directory");
//You can simply copy files like above 
 
//Creating Directory
var d2 = new SMF.IO.File(SMF.IO.applicationDataDirectory, "tmp2");
success = d2.createDirectory(); 
//If it is true that means directory created successfully. 

File Content

The example below explains how the content is read and written:

var logFile = new SMF.IO.File(SMF.IO.applicationDataDirectory,
			"logs" + SMF.IO.pathSeparator + "logFile.txt");

Write

function Page1_edtLogEntry_OnReturnKey(e) {
	var streamWriter = logFile.openStream(SMF.IO.StreamType.APPEND);
	streamWriter.write(this.text + SMF.IO.lineEnding); // instead of lineEnding you can use \n too
	streamWriter.close(); //releases the file
	streamWriter = null; //releases memory;
}

Read

function Page1_DisplayLog_OnReturnKey(e) {
	//creating (reader) stream object from path
	var streamReader = new SMF.IO.FileStream(SMF.IO.applicationDataDirectory,
			"logs" + SMF.IO.pathSeparator + "logFile.txt",
			SMF.IO.StreamType.READ);
	alert({
		message : streamReader.readToEnd(), //Whole file content read at once
		title : "File content"
	});
	streamReader.close(); //releases the file
}

SMF.IO

The members of SMF.IO are listed in API Docs. Here are the list of the members and their purposes:

  • File – JavaScript function wrapper object for both files and directories
  • FileSteam – JavaScript function wrapper object for reading and writing file contents.
  • SteamType – JavaScript enumerator object for defining stream type.
  • applicationDataDirectory – Static File object instance for Path to the data directory of an application.
  • applicationCacheDirectory – Static File object instance for Path to the internal cache directory of an application.
  • applicationResources – Static File object instance for Path to the resource directory of an application.
  • applicationTemporaryData – Static File object instance for Path for the temporary directory of an application.
  • lineEnding – JavaScript string which gives line ending of the environment.
  • pathSeparator – JavaScript string which gives the path separator of the environment.
  • getExternalStorages – JavaScript function which returns an array of external storage list.

Share Files

We can share files, images and texts on the Smartface as below:

var img = new SMF.Bitmap(obj);
var file = new SMF.IO.File(filePath);
var text = "Text to share";
var items = [file, img, text]; // a file , an image and a text. In Android, only use first element of the array.
Device.share({
	items : items,
	keys : {
		"subject" : "A subject"
	},
	onSuccess : function (e) {
		alert("Succesfully shared");
	},
	exclude : [
		Social.ActivityType.ASSIGNTOCONTACT,
		Social.ActivityType.SAVETOCAMERAROLL
	],
	onError : function (e) {
		alert("Error: " + e.message);
	}
});


getExternalStorages
This method is the listing the external storages. As iOS does not support external storages, this will work for Android only.

Android will list the plugged-in (connected & online) external storages in the following order if they are available:

Emulated SD
External SD card
USB storages

To get better understanding of "File & Images" operations, please refer to the following guides after reading this guide.