Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Titanium Creating Image file: file.write(blob) not creating the correct file

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.

like image 767
vaibhav Avatar asked Nov 13 '22 09:11

vaibhav


1 Answers

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);
like image 128
Bogdan Trusca Avatar answered Nov 16 '22 04:11

Bogdan Trusca