【问题标题】:How save data using Tizen如何使用 Tizen 保存数据
【发布时间】:2021-02-15 15:36:41
【问题描述】:

我正在尝试在三星手表(可穿戴 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);
   });

但是,当使用模拟器尝试时,不会创建任何文件。有人知道我如何创建一个文件来保存记录的事件吗?

非常感谢您的帮助!!!

雨果

【问题讨论】:

    标签: tizen tizen-web-app tizen-emulator


    【解决方案1】:

    您是否设置了权限? 如果没有,您必须设置

    mediastorage

    externalstorage

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

    【讨论】:

      【解决方案2】:

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

      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));
          });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-06
        • 2018-05-09
        • 2015-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多