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

独自空忆成欢 提交于 2019-12-07 05:21:38

问题


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.


回答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);



回答2:


You need to close the file once you finished writing.

test.close();


来源:https://stackoverflow.com/questions/9377378/titanium-creating-image-file-file-writeblob-not-creating-the-correct-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!