如何使用 Tizen 保存数据

问题描述

我正在尝试在三星手表(可穿戴 4.0)上开发一个小应用程序。这个想法很简单,人们每次打开应用程序时都必须回答一个问题。 这是基本的HTML代码

<html>

<style>
h1 {color: white; font-family: courier new; font-size:150%; text-align: center; position: absolute; top: 5%; left: 14%}
body {background-color: SkyBlue;}
#button1 {font-size: 20px; color:white; position:absolute; top: 30%; left: 23%; width: 200px; height: 55px; background-color: black; border-color: black;}
#button2 {font-size: 20px; color:white; position:absolute; top: 70%; left: 23%; width: 200px; height: 55px; background-color: black; border-color: black;}
</style>

<body>
<h1>Es tu concentré <br> sur un élément extérieur?</h1>
<button id="button1" onclick="location.href = 'Third.html';">Mon attention est captée</button>
<button id="button2" onclick="location.href = 'Third.html';">Rien ne capte mon attention</button> 

</body>

</html>

在正文中,我放置了一个脚本,其中将事件(单击特定按钮)记录在一个数组中:

var data = [];

document.getElementById("button1").addEventListener("click",function(){
    date = new Date();
    data.push("Concentré");
    data.push(date);
    document.getElementById("demo").innerHTML = data;
    });

document.getElementById("button2").addEventListener("click",function(){
    date = new Date();
    data.push("Pas Concentré");
    data.push(date);
    document.getElementById("demo1").innerHTML = data;
    });

最后,我想将变量“data”保存在一个 txt 文件中,所以我复制/浪费了我在 Tizen 文档中找到的内容

var documentsDir; 
documentsDir.createFile("MWRecord.txt");
function onsuccess(files) {
  var testFile = documentsDir.createFile("test.txt");
  if (testFile != null) {
     testFile.openStream("w",function(fs) {
         fs.write(data);
         fs.close();
       });
   }
 };

 function onerror(error) {
   console.log("The error " + error.message + " occurred when listing the files in the selected folder");
 };
 
 tizen.filesystem.resolve(
   'documents',function(dir) {
     documentsDir = dir;
     dir.listFiles(onsuccess,onerror);
   });

然而,当用模拟器尝试时,没有创建文件。有人知道我如何创建一个文件来保存记录的事件吗?

非常感谢您的帮助!!!

雨果

解决方法

你设置权限了吗? 如果没有,你必须设置

mediastorage

externalstorage

权限,以便应用程序保存文件。

,

我注意到的第一个问题是,您的代码在第二行使用“undefined”作为对象,这会导致代码执行中断。

var documentsDir; 
documentsDir.createFile("MWRecord.txt");  /// documentsDir === undefined

其次,我不确定您是否需要调用 listFiles() 或者这是一个复制粘贴问题,在您的描述中,如果是故意的,我没有找到答案。

要创建新文件并用一些数据填充它,您需要使用回调正确处理所有异步操作:

tizen.filesystem.resolve(
    'documents',function (dir) {
        try {
            var testFile = dir.createFile("test.txt");
            testFile.openStream("w",function (fs) {
                fs.write("some data as a string");
                fs.close();
                // at this point your file is created and content is written
                console.log("Success")
            },function (e) {
                    // TODO handle openStream() errors here
                    console.log("openStream error occurrred " + JSON.stringify(e));
                });
        } catch (e) {
            // TODO handle createFile() errors here
            console.log("createFile error occurrred " + JSON.stringify(e));
        }
    },function (e) {
        // TODO handle resolve() errors here
        console.log("resolve error occurrred " + JSON.stringify(e));
    });

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...