How to read and write a file in local/internal storage of android device

You must Login before you can answer or comment on any questions.

Hi everyone i have an requirement where i have to save a file if not exists... if exists then read and write a file from the internal storage of android device.. i have used the following code but it is neither creating a file nor reading/writing... can somebody help me .... my code is

var data = [{'V':10},{'P':20},{'K':30},{'A':40},{'Kr':50},{'S':60}]; var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory , 'Vijay.txt'); if(!file.exists){ file.createFile(); } file.write(JSON.stringify(data));

if (Ti.Filesystem.isExternalStoragePresent) var readFile = Ti.Filesystem.getFile(file).read() ; // new_path = Titanium.Filesystem.externalStorageDirectory + Date.now().toString() + ".csv" ; f = Ti.Filesystem.getFile(Titanium.Filesystem.externalStorageDirectory,'Vi') ; f.createDirectory(); var newFile = Titanium.Filesystem.getFile(f.nativePath,'ABC.txt');

        result = newFile.write(readFile) ;
        if ( result === false){
  Ti.API.debug("Write Error");
 }
 Ti.API.debug("Write complete? "  + result);
— asked 2 years ago by vijay Kumar
2 Comments
  • what exact.. you want to do.. please provide some more information

    — commented 2 years ago by Jayesh Joshi

  • Actually, by using Android device... once i get all the objects in an array, i need to create a file if not exist into the local android directory and then write the array objects into that file .. once writing is done i need to upload that file from that local android directory to the cloud

    — commented 2 years ago by vijay Kumar

2 Answers

var data = [{ 'V' : 10 }, { 'P' : 20 }, { 'K' : 30 }, { 'A' : 40 }, { 'Kr' : 50 }, { 'S' : 60 }];

var file = null;

var Myfullfolder = null;

if (Titanium.Platform.osname != 'android') {

file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'Vijay.txt');

Myfullfolder = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'Vi');

} else {

if (Ti.Filesystem.externalStorageDirectory == true) {


    file = Titanium.Filesystem.getFile(Titanium.Filesystem.getExternalStorageDirectory(), 'Vijay.txt');

    Myfullfolder = Ti.Filesystem.getFile(Titanium.Filesystem.getExternalStorageDirectory(), 'Vi');

} 

else {

    file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'Vijay.txt');

    Myfullfolder = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'Vi');
}

}

if (!file.exists) {

file.createFile();

}

file.write(JSON.stringify(data));

if (!Myfullfolder.exists()) {

Myfullfolder.createDirectory();

}

var newFile = Titanium.Filesystem.getFile(Myfullfolder.resolve(), 'ABC.txt');

if (!newFile.exists) {

newFile.createFile();

}

try {

var readFile = Titanium.Filesystem.getFile(file.nativePath).read().text;

var result = newFile.write(readFile);

} catch(erp) {

alert(erp)

}

where i should put my file to make my application read it...i am using android 4.4

Your Answer

Think you can help? Login to answer this question!