ChildOfCode


Code, Maker, Robotic, Open Source. Knowledge Bases


Titanium Studio Save the Apps data in the Filesystem

In IOS is only Internal Storage ,but in Android is diffrent since some android device can use sd-card

theClass.prototype.WriteLog = function(theJSON){

    Ti.API.info('WriteLog = ' + new Date() );
    var thisClass = this;
    var jsonDir = '';
    var dateObj = new Date();
    var month = dateObj.getUTCMonth() + 1; //months from 1-12
    var day = dateObj.getUTCDate();
    var year = dateObj.getUTCFullYear();
    var theDate = year + '' + month + '' + day;


    if(thisClass.Parent.OSName == "iphone" || thisClass.Parent.OSName == "ipad"){
        jsonDir = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory ,'json');
        //write into a file
        this.jsonFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'json','pocsjson'+theDate+'.txt');

    }else{
        jsonDir = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory,'json');
        this.jsonFile = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory,'json','pocsjson'+theDate+'.txt');
    }

    //create folder
    if (! jsonDir.exists()) {
        jsonDir.createDirectory();
    }

    //serialize
    this.jsonFile.write( JSON.stringify(theJSON), true); // write to the file
     //this.jsonFile.deleteFile(); // delete the file

    //read that file       
    var contents = this.jsonFile.read();
    Ti.API.info('jsonFile.read() as a blob: '+contents); // useful if contents are binary

};

tiapp.xml Setup

<android xmlns:android="http://schemas.android.com/apk/res/android">  
    <manifest>
        <application android:theme="@style/Theme.Titanium"/>
            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
            <uses-permission android:name="android.permission.STORAGE" />
            <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        </manifest>
</android>