将文件写入 Tizen 文件系统
Write file to Tizen filesystem
我正在尝试使用 Samsung Gear 2 设备将一些数据写入文本。我正在使用 Tizen SDK。我有以下代码,是从 Tizen turorial for filesystem 中找到的。我通常可以在文档文件夹中用我的代码创建一个文件,但似乎我无法向该文件写入内容。我的文件是空的。
编辑:我的代码可以写入心率值,但似乎每次都在文件的相同位置重新写入。最后我只得到一个心率值。怎么可能在app 运行.
期间写入所有心率值
window.onload = function () {
var initial = new Date().getTime();
var dataLength = 500;
var dps = []; // dataPoints
var historyDataLength = 5;
var history = [];
var temp = "nada";
function onsuccess(files) {
var testFile = null;
try{
testFile = documentsDir.createFile("test.txt");
if (testFile !== null) {
testFile.openStream(
"w",
function(fs){
fs.write(temp);
fs.close();
}, function(e){
console.log("Error " + e.message);
}, "UTF-8"
);
}
}
catch (e) { // file already exist -> append content
testFile = documentsDir.resolve('test.txt');
if(testFile !== null)
{
testFile.openStream(
"w",
function(fs){
fs.write(temp);
fs.close();
}, function(e){
console.log("Error " + e.message);
}, "UTF-8"
);
}
}
}
function onerror(error) {
console.log("The error " + error.message + " occurred when listing the files in the selected folder");
}
var chart = new CanvasJS.Chart("chartContainer",{
title :{
fontColor: "#ccc",
text: "Heart Rate"
},
backgroundColor: "#222",
data: [{
color: "#CD5C5C",
type: "line",
dataPoints: dps
}]
});
var lastSecond = -1;
var updateChart = function (heartrate) {
time = new Date().getTime() - initial;
console.log("[" + time + ", " + heartrate + "]");
temp = heartrate;
console.log("tempVar"+ temp);
tizen.filesystem.resolve(
'documents',
function(dir){
documentsDir = dir; dir.listFiles(onsuccess,onerror);
}, function(e) {
console.log("Error" + e.message);
}, "a"
);
dps.push({
x: time / 1000.0,
y: heartrate
});
if (dps.length > dataLength)
{
dps.shift();
}
var second = Math.round(time / 1000.0);
console.log(history.length);
if(lastSecond != second) {
// TODO use avg heart rate instead of smapshot.
history.push({
x: second,
y: heartrate
});
if(history.length > historyDataLength) {
history.shift();
}
lastSecond = second;
}
if(dps.length >= dataLength) {
chart.render();
}
var hrchart = "<center>" + heartrate + "bps</center><table width='100%' cellpadding=4px>";
for(var i = history.length - historyDataLength; i >= 0 && i < history.length; i++) {
hrchart += "<tr><td align='right' width='50%'>" + history[i].x + "s</td><td width='50%'>" + history[i].y + "bps</td></tr>";
}
hrchart += "</table>";
$('#textbox').html(hrchart);
};
updateChart(0);
updateChart(250);
for(var i = 0; i < dataLength; i++) {
updateChart(0);
}
document.addEventListener('tizenhwkey', function(e) {
if(e.keyName == "back")
tizen.application.getCurrentApplication().exit();
});
window.webapis.motion.start("HRM", onchangedCB);
function onchangedCB(hrmInfo)
{
if(hrmInfo.heartRate > 0) {
// add eventListener for tizenhwkey
document.addEventListener('tizenhwkey', function(e) {
if(e.keyName == "back")
tizen.application.getCurrentApplication().exit();
});
updateChart(hrmInfo.heartRate);
} else {
$('#textbox').html("No heart rate detected.");
}
}
}
在控制台中,打印了所有 console.log 消息。其次,如何使用按钮关闭写入过程?这是停止写入过程的命令。
我认为您遇到了可变范围问题。线
var documentsDir;
在您的 window.onload 函数中定义了一个局部变量(将为空)。相反,您希望此函数使用您在 tizen.filesystem.resolve() 函数中创建的 documentsDir 变量。注释掉 onload 中的 var 行(或将其移至 tizen.filesystem.resolve(
行之前,以便 documentsDir 在 onload 函数内外相同。
我正在尝试使用 Samsung Gear 2 设备将一些数据写入文本。我正在使用 Tizen SDK。我有以下代码,是从 Tizen turorial for filesystem 中找到的。我通常可以在文档文件夹中用我的代码创建一个文件,但似乎我无法向该文件写入内容。我的文件是空的。 编辑:我的代码可以写入心率值,但似乎每次都在文件的相同位置重新写入。最后我只得到一个心率值。怎么可能在app 运行.
期间写入所有心率值window.onload = function () {
var initial = new Date().getTime();
var dataLength = 500;
var dps = []; // dataPoints
var historyDataLength = 5;
var history = [];
var temp = "nada";
function onsuccess(files) {
var testFile = null;
try{
testFile = documentsDir.createFile("test.txt");
if (testFile !== null) {
testFile.openStream(
"w",
function(fs){
fs.write(temp);
fs.close();
}, function(e){
console.log("Error " + e.message);
}, "UTF-8"
);
}
}
catch (e) { // file already exist -> append content
testFile = documentsDir.resolve('test.txt');
if(testFile !== null)
{
testFile.openStream(
"w",
function(fs){
fs.write(temp);
fs.close();
}, function(e){
console.log("Error " + e.message);
}, "UTF-8"
);
}
}
}
function onerror(error) {
console.log("The error " + error.message + " occurred when listing the files in the selected folder");
}
var chart = new CanvasJS.Chart("chartContainer",{
title :{
fontColor: "#ccc",
text: "Heart Rate"
},
backgroundColor: "#222",
data: [{
color: "#CD5C5C",
type: "line",
dataPoints: dps
}]
});
var lastSecond = -1;
var updateChart = function (heartrate) {
time = new Date().getTime() - initial;
console.log("[" + time + ", " + heartrate + "]");
temp = heartrate;
console.log("tempVar"+ temp);
tizen.filesystem.resolve(
'documents',
function(dir){
documentsDir = dir; dir.listFiles(onsuccess,onerror);
}, function(e) {
console.log("Error" + e.message);
}, "a"
);
dps.push({
x: time / 1000.0,
y: heartrate
});
if (dps.length > dataLength)
{
dps.shift();
}
var second = Math.round(time / 1000.0);
console.log(history.length);
if(lastSecond != second) {
// TODO use avg heart rate instead of smapshot.
history.push({
x: second,
y: heartrate
});
if(history.length > historyDataLength) {
history.shift();
}
lastSecond = second;
}
if(dps.length >= dataLength) {
chart.render();
}
var hrchart = "<center>" + heartrate + "bps</center><table width='100%' cellpadding=4px>";
for(var i = history.length - historyDataLength; i >= 0 && i < history.length; i++) {
hrchart += "<tr><td align='right' width='50%'>" + history[i].x + "s</td><td width='50%'>" + history[i].y + "bps</td></tr>";
}
hrchart += "</table>";
$('#textbox').html(hrchart);
};
updateChart(0);
updateChart(250);
for(var i = 0; i < dataLength; i++) {
updateChart(0);
}
document.addEventListener('tizenhwkey', function(e) {
if(e.keyName == "back")
tizen.application.getCurrentApplication().exit();
});
window.webapis.motion.start("HRM", onchangedCB);
function onchangedCB(hrmInfo)
{
if(hrmInfo.heartRate > 0) {
// add eventListener for tizenhwkey
document.addEventListener('tizenhwkey', function(e) {
if(e.keyName == "back")
tizen.application.getCurrentApplication().exit();
});
updateChart(hrmInfo.heartRate);
} else {
$('#textbox').html("No heart rate detected.");
}
}
}
在控制台中,打印了所有 console.log 消息。其次,如何使用按钮关闭写入过程?这是停止写入过程的命令。
我认为您遇到了可变范围问题。线
var documentsDir;
在您的 window.onload 函数中定义了一个局部变量(将为空)。相反,您希望此函数使用您在 tizen.filesystem.resolve() 函数中创建的 documentsDir 变量。注释掉 onload 中的 var 行(或将其移至 tizen.filesystem.resolve(
行之前,以便 documentsDir 在 onload 函数内外相同。