Titanium 创建图像文件:file.write(blob) 未创建正确的文件
我正在尝试使用 Titanium 1.8.1 读取 .PNG 文件 这是我读取文件的代码。
var f = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'KS_nav_views.png');
var blob = f.read();
当我使用上面的 blob 对象创建新文件时,创建的新文件与原始文件不同。 这是我创建新文件的代码。
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);
outFile.png 已创建,但问题是它不是有效的图像文件。文件的大小约为 53 字节,而我的输入文件为 1kb。
如果我们使用简单的文本文件作为输入并尝试创建重复的输出文件,相同的代码可以正常工作。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不需要做 read() 这样做:
You do not need to do read() do it like this:
完成写入后,您需要关闭文件。
You need to close the file once you finished writing.