我使用的是FileWriter,它工作得很好,除了当我写各种大小到大约3MB的大文件时,日志中的这些消息。
我看了一下写的源代码,FileUtils.java函数没有使用getThreadPool()接口(阅读器使用)。
作为一个测试,我认为我应该调整文件写入器以使用runnable接口,并能够编译和执行代码-不幸的是,logcat消息仍然显示……
到目前为止,我得到的阻塞时间在25ms到1200ms之间。我还没有运行任何认真的比较测试来确定这个更改是否真的有什么不同--我只是在寻找是否没有logcat消息。
下面的这些改变会有什么真正的不同吗?
这些信息是我应该担心的吗?
我的java是非常基础的--但下面是我在阅读器实现之后所做的修改。
else if (action.equals("write")) {
this.write(args.getString(0), args.getString(1), args.getInt(2), args.getBoolean(3), callbackContext);
}
/* this is the original code
else if (action.equals("write")) {
long fileSize = this.write(args.getString(0), args.getString(1), args.getInt(2), args.getBoolean(3));
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, fileSize));
} */
在write函数中,如下所示...
public void write(String filename, final String data, final int offset, final boolean isBinary, final CallbackContext callbackContext) throws FileNotFoundException, IOException, NoModificationAllowedException {
if (filename.startsWith("content://")) {
throw new NoModificationAllowedException("Couldn't write to file given its content URI");
}
final String fname = FileHelper.getRealPath(filename, cordova);
this.cordova.getThreadPool().execute(new Runnable() {
public void run() {
Log.d(LOG_TAG, "Starting write");
try {
boolean append = false;
byte[] rawData;
if (isBinary) {
rawData = Base64.decode(data, Base64.DEFAULT);
} else {
rawData = data.getBytes();
}
ByteArrayInputStream in = new ByteArrayInputStream(rawData);
FileOutputStream out = new FileOutputStream(fname, append);
byte buff[] = new byte[rawData.length];
in.read(buff, 0, buff.length);
out.write(buff, 0, rawData.length);
out.flush();
out.close();
Log.d(LOG_TAG, "Ending write");
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, rawData.length));
} catch (IOException e) {
Log.d(LOG_TAG, e.getLocalizedMessage());
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, NOT_READABLE_ERR));
}
}
});
}
发布于 2014-05-04 15:50:52
是的,这些消息很重要,你应该使用后台线程来处理复杂的任务,比如文件写入。这个问题的原因是这些任务阻塞了cordova,例如,你可能会遇到UI延迟。
如果您的下一步操作依赖于此任务的完成,我建议您使用回调方法。
https://stackoverflow.com/questions/18139898
复制相似问题