5

I am trying to read a .PNG file using Titanium 1.8.1 Here is my code to read file.

var f = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'KS_nav_views.png');
var blob = f.read();

When I create a new file using the above blob object, the new file thus created is not same as the original file. Here is my code to create the new file.

var outputDir = Titanium.Filesystem.getFile(Titanium.Filesystem.externalStorageDirectory,'output');
outputDir.createDirectory();
var newFile = Titanium.Filesystem.getFile(outputDir.nativePath,'outFile.png');
var test = newFile.write(blob);
if ( test === false){
      Ti.API.debug("Write Error");
}
Ti.API.debug("Write complete? "  + test);

The outFile.png gets created but the problem is that It is not a valid image file. Also the size of the file is around 53 bytes, whereas my input file was 1kb.

The same code works fine if we use a simple text file as input and try to create duplicate output file.

vaibhav
  • 550
  • 5
  • 20

2 Answers2

1

You do not need to do read() do it like this:

var t = Titanium.Filesystem.getFile(tempDataDirectory, 'a.json');
var o = Titanium.Filesystem.getFile(onlineDataDirectory, 'b.json');
o.write(t);
Bogdan T Stancu
  • 404
  • 5
  • 6
0

You need to close the file once you finished writing.

test.close();
bsavas
  • 124
  • 5
  • 1
    Hi bsavas, test is a boolean not file object. also Titanium has no such method called close on file object. – vaibhav Feb 23 '12 at 11:18
  • I see. Sorry, I thought It was file-stream. Maybe you should give it a try with FileStream object. http://developer.appcelerator.com/blog/2011/05/titanium-mobile-intro-series-streams.html – bsavas Feb 24 '12 at 09:57