我正在创建一组Haxe函数,用于将文本和图像保存到文件中。这些功能在Windows和Android上运行良好;但是,测试人员告诉我,尝试保存图像会在iOS和Mac上产生此错误:
ERROR: Failure type not string @ ./File.cpp:123
这是两个函数的代码。
public static function savePNG(path:String, image:BitmapData, ?whenDone:Bool->Void):Void {
if (path.substr(path.length - 4).toLowerCase() != ".png") { path += ".png"; }
// Flash: Not possible to save; error out
#if flash
trace("ERROR: File IO cannot be accessed on Flash.");
if (whenDone != null)
whenDone(false);
#elseif js
trace("ERROR: File IO cannot be accessed on HTML5.");
if (whenDone != null)
whenDone(false);
// Windows, Mac, Linux, iOS, and Android: Use the "saveText" function with the converted file
#else
var b:ByteArray = image.encode("png", 1);
saveText(path, b.toString(), whenDone);
#end
}
public static function saveText(path:String, content:String, ?whenDone:Bool->Void):Void {
var success:Bool = true;
var path2:String = "";
path = "/assets/data/" + path;
var a:Array<String> = DataUtils.subfold(path);
// Flash or HTML5: Not possible to save; error out
#if (flash || js)
success = false;
#if flash
trace("ERROR: File IO cannot be accessed on Flash.");
#else
trace("ERROR: File IO cannot be accessed on HTML5.");
#end
// iOS and Android: Attempt to save to the storage directory
#elseif mobile
if (!FileSystem.exists(SystemPath.userDirectory + "/" + a[0])) {
FileSystem.createDirectory(SystemPath.userDirectory + "/" + a[0]);
}
path2 = SystemPath.userDirectory + "/" + a[0] + "/" + a[1];
try {
File.saveContent(path2, Std.string(content));
} catch (e:Dynamic) {
success = false;
trace("ERROR: " + e);
errorify(e);
}
// Windows, Mac, and Linux: Save straight to the "assets/data/" folder
#else
if (!FileSystem.exists(FileSystem.fullPath(a[0]))) {
FileSystem.createDirectory(FileSystem.fullPath(a[0]));
}
path2 = FileSystem.fullPath(a[0] + "/" + a[1]);
try {
File.saveContent(path2, Std.string(content));
} catch (e:Dynamic) {
success = false;
trace("ERROR: " + e);
errorify(e);
}
#end
if (whenDone != null)
whenDone(success);
}
错误来自最后一行trace("ERROR: " + e);
。我会排除故障,但我没有Mac或iOS设备,而且我不确定测试仪需要哪些信息。
底线:如果此代码中存在明显错误,如何修复?如果没有,我需要询问哪些故障排除信息?
答案 0 :(得分:1)
我的猜测是,在Bytes-&gt;字符串转换过程中出现了一些问题,这里完全没有必要。我建议你删除它并使用FileOutput的二进制版本。如果您使用的是最新版本的lime,则可以将ByteArray转换为Bytes(因为ByteArray基础类型是Bytes)并将其写入FileOutput。以下是&#39;写&#39;部分应该看起来像
/**
* Save bytes as a file
* @param path
* @param content
* @return true if succeed, false overwise
*/
static function saveBytes(path:String, content:Bytes):Bool
{
var success = false;
var fo:FileOutput = null;
try {
//open binary file and write bytes
fo = File.write(path, true);
fo.writeBytes(content, 0, content.length);
success = true;
} catch (e:Dynamic) {
trace("ERROR: " + e);
errorify(e);
}
//file output should be closed in any case
try {
if (fo != null)
fo.close();
} catch (e:Dynamic) {
trace("ERROR: " + e);
errorify(e);
}
return success;
}
另外,编码部分
/* insert your path construction here */
var b:ByteArray = image.encode("png", 1);
var success = saveBytes(path, (b:Bytes));
if (whenDone != null)
whenDone(success);