0

我正在三星 Gear S2 上开发本机应用程序,旨在将设备上传感器(加速度计)的数据记录到本地文件系统中。

我尝试了目录(/opt/usr/media/documents/text.txt),但它没有在设备上创建文件。

我启用了特权:http ://tizen.org/privilege/mediastorage 。下面的代码片段:

static char* write_file(const char* buf)
{
    FILE *fp;
    fp = fopen("/opt/usr/media/documents/text.txt","w");
    fputs(buf,fp);
    fclose(fp);
}

static void btn_write_cb(void *data, Evas_Object *obj, void *event_info)
{
    appdata_s *ad = data;
    buf="string to write";
    write_file(buf);
}

写入方法是什么?或 Web 应用程序适用于此任务?谢谢

4

4 回答 4

2

最后,我可以通过 Tizen Native App Development 在我的 Samsung Gear S2 Classic 中写入文件。带地址:/opt/usr/media/text.txt。并使用了特权http://tizen.org/privilege/mediastorage。这是我的代码片段。


static void
app_get_data(const char *res_file_in, char *res_path_out, int res_path_max)
{
    char *res_path = NULL;
    res_path= "/opt/usr/media/";
    if (res_path) 
    {
        snprintf(res_path_out, res_path_max, "%s%s", res_path, res_file_in);
        //free(res_path); //Disabling this statement works for me.
    }
}

static char*
write_file(const char* filepath, const char* buf)
{
    FILE *fp;
    fp = fopen(filepath, "w"); 
    fputs(buf, fp);
    fclose(fp);
}

static void
btn_write_cb(void *data, Evas_Object *obj, void *event_info)
{
    appdata_s *ad = data;
    char *buf = NULL;
    buf="Hello";
    char filepath[PATH_MAX] = { 0, };
    app_get_data("text.txt", filepath, PATH_MAX);
    write_file(filepath, buf);
}
于 2017-08-12T04:04:38.837 回答
0

在这种情况下,Web API 可能会起作用。但是如果您不想切换到 Web App Development,那么您可以尝试使用Preference API。它将使您能够将传感器数据存储在共享内存中并从中检索数据。

您也可以通过以下链接了解如何详细使用 Preference API。

  1. 参考。链接 1
  2. 参考。链接 2

希望它会工作。

于 2017-08-11T10:06:16.617 回答
0
  1. 在 Tizen 中,即使使用http://tizen.org/privilege/mediastorage. 首先,尝试将文件写入您的应用程序肯定具有写入权限的文件app_get_shared_data_path()夹 ( /opt/usr/apps/<your_app_package_name>/shared/data)(可能是您的应用程序没有对文件夹的写入权限/opt/usr/media/)。
  2. /opt/usr/media/documents无论如何,如果文件的所有路径 ( ) 不存在,则无法创建文件(在我的 Gear S2 上,该路径没有“文档”文件夹)。
于 2017-08-10T09:00:40.097 回答
0

也添加这些权限

<tizen:privilege name="http://tizen.org/privilege/filesystem.read"/>
<tizen:privilege name="http://tizen.org/privilege/filesystem.write"/>

也许您可以更改路径/opt/usr/media/text.txt并检查文件是否已创建。

Web 应用程序实际上可以很好地用于此目的,这是一个示例代码片段:

function fileStorage() {
    var documentsDir, newFile;

    function onOpenSuccess(fs) {
        fs.write("This is gonna written into the file");
        fs.close();
    }

    try {
        tizen.filesystem.resolve('internal0', function(result) {
            documentsDir = result;
            newFile = documentsDir.createFile('filename.txt');
            if (newFile) {
                newFile.openStream('rw', onOpenSuccess, null, 'UTF-8');
            }
        });
    } catch (e) {
        console.log(e);
    }
}
于 2017-08-10T09:45:18.150 回答