ZEESHAN
ZEESHAN

Reputation: 319

How to write text file on Tizen Gear S2 local file system?

I'm developing native app on Samsung Gear S2 which intended to record data from sensors (accelerometer) on device into local file system.

I try the directory ( /opt/usr/media/documents/text.txt ) but its not creating the file on device.

I have enabled the privilege: http://tizen.org/privilege/mediastorage. Code snippet below:

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

What is the write way to do? OR Web app work for this task? Thanks

Upvotes: 0

Views: 1360

Answers (4)

ZEESHAN
ZEESHAN

Reputation: 319

Finally I am able to write file in my Samsung Gear S2 Classic by Tizen Native App Development. With address: /opt/usr/media/text.txt. And used the privilege http://tizen.org/privilege/mediastorage. Here is my code snippet.


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

Upvotes: 2

Shaswati Saha
Shaswati Saha

Reputation: 768

Web API may work in this case. But if you don't want to switch in Web App Development, then you may try with Preference API. It'll give you the facility to store sensor data in shared memory and retrieve data from that.

You may also go through links below to know how to use Preference API in details.

  1. Ref. Link 1
  2. Ref. Link 2

Hope it'll work.

Upvotes: 0

Aldrin Joe Mathew
Aldrin Joe Mathew

Reputation: 482

Add these privileges too

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

Maybe you can change the path to /opt/usr/media/text.txt and check if the file has created or not.

Web app works for this purpose pretty well actually, here's a sample code snippet :

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

Upvotes: 0

Andrii Omelchenko
Andrii Omelchenko

Reputation: 13353

  1. In Tizen You can't create file somewhere even with http://tizen.org/privilege/mediastorage. Try, at first, write file to app_get_shared_data_path() folder (/opt/usr/apps/<your_app_package_name>/shared/data) to which Your app definitely has write access (may be Your app hasn't write access to /opt/usr/media/ folder).
  2. Anyway, You can't create file if all path (/opt/usr/media/documents) to it don't exists (on my Gear S2 there is no "documents" folder at that path).

Upvotes: 0

Related Questions