Voting: 
Vote up!
0 Votes
1 Replies
Unanswered

File creation issue - Studio 6.5

Question asked by Magesh Rathnam on Apr 04, 2017 11:29 EDT

Hi – Am using Kony Studio 6.5. I have been trying to use file/directory creation. It returns false, but am not sure what is the reason. Am trying in Android and iOS.  Can someone guide me on how to see the issue? I have set 'WRITE_EXTERNAL_STORAGE' permission as well, in android, though am not sure if it is really needed. I have problem in ios as well. It goes into file not exists or dir not exists loop, but does not create them. I think file creation fails, because dir creation was unsuccessful. Code snippet: var path = kony.io.FileSystem.getDataDirectoryPath(); var testFilePath = path+"/test/test.txt"; function formPreInit(){ kony.print("Inside Form Pre Init"); var dirLoc = path + "/test"; kony.print("dirLoc is: " + dirLoc); var directoryPath = new kony.io.File(dirLoc); if(!directoryPath.exists()) { //Create the new dir, if it does not exist. var dirPath = new kony.io.File(path); var dirCreationSuccess = dirPath.createDirectory(test); kony.print("dirCreationSuccess :: " + dirCreationSuccess); var dataToWrite = "Write test"; saveDataToFile(dataToWrite); } //Create the file at the path, if it does not exist //Write to the file, the data passed function saveDataToFile(dataToWrite) { kony.print("Inside saveDataToFile"); var filePath = new kony.io.File(testFilePath); if (!filePath.exists()){ kony.print("File does not exist. Creating new one"); var success = filePath.createFile(); alert("File created " + success); } var dataAdded = filePath.write(dataToWrite,true); alert("IS DATA ADDED >>>>>>>"+dataAdded); }  

Posted In
Kony Inc Kony Studio
Vote up!
0 votes
Vote down!

Hi Magesh,

Below is the code to create file in the file system,

var path = kony.io.FileSystem.getDataDirectoryPath();                                                                                                            //create the file in directory photos                                                                                                                                           var myfile = kony.io.FileSystem.getFile(path + "/photos/" + lMediaName + ".jpg");                                                                           var filecreated = myfile.createFile();

Where in case of Windows and Android if the given directory is not availabel then system will create the directory and will write the file. where in case of iOS if the directory is not avaialble it will return false.

To make if work in iOS

Please add the below code snippet before

var myfile = kony.io.FileSystem.getFile(path + "/photos/" + lMediaName + ".jpg");"

var myDirectory = kony.io.FileSystem.getFile(path + "/photos/");
var directorycreated = myDirectory.createDirectory();

As in case of iPhone, file won't be created if directory does not exist.

 

Thanks

Replied by: Apr 06, 2017 00:41 EDT
Bronze