Languages

Menu
Sites
Language
How to read “resource” files added via Tizen IDE
I'm giving my first steps on programming web apps for Tizen wearable devices . This is what I want to do : 1. By using the Tizen IDE , add a file , e.g. x.txt , under a folder , e.g. data/text (i.e. relative path from project root is data/text/x.txt ) 2. At run-time , I want to read the contents of x.txt file 3. ... and do some extra processing with it . I thought I could just read wgt-package virtual root but my code (after fixing it) returns no file at that location . How could I do this ? BTW , I've been testing on the web simulator . p.s. I'm aware of the fact that this is quite simple , so I guess this should be documented somewhere but I just could not find a reference after searching for a while (since yesterday), so I'm hoping someone can help me put my efforts on the right track Thanks in advance !

Responses

5 Replies
Olemis Lang
Since I could not find a way to edit the original post I'm rewriting it below

I'm giving my first steps on programming web apps for Tizen wearable devices . This is what I want to do :

  1. By using the Tizen IDE , add a file , e.g. x.txt , under a folder , e.g. data/text (i.e. relative path from project root is data/text/x.txt )
  2. At run-time , I want to read the contents of x.txt file
  3. ... and do some extra processing with it .

I thought I could just read wgt-package virtual root but my code (after fixing it) returns no file at that location .

How could I do this ? BTW , I've been testing on the web simulator .

p.s. I'm aware of the fact that this is quite simple , so I guess this should be documented somewhere but I just could not find a reference after searching for a while (since yesterday), so I'm hoping someone can help me put my efforts on the right track

Thanks in advance !

Seoghyun Kang

Dear Olemis Lang,

 

When I watched your code at stackoverflow, you are using the Tizen device API.  (tizen.filesystem.resolve)

 

However, it is impossible to use the Tizen device API on the web simulator because it does not support the Tizen device API.

So if you want to use the Tizen device API, please use the Tizen emulator or Tizen Device.

 

After you test your code on Tizen emulator, please let us know if there is any problem.

 

Please refer it.

Thanks.

 

AVSukhov

Hello,

Try to use following location for resolve method:
"wgt-package/data/text/x.txt"

byonggon chun
function onsuccess(files) {
   for (var i = 0; i < files.length; i++) {
     console.log("File Name is " + files[i].name); // displays file name

     if(file[i].name = "your_txt_file.txt"){
        //do something here. file[i].readAsText(....)
     }
   }
 }

 function onerror(error) {
   console.log("The error " + error.message + " occurred when listing the files in the selected folder");
 }

 tizen.filesystem.resolve(
     "wgt-package/data/text",
     function(dir) {
       documentsDir = dir; dir.listFiles(onsuccess,onerror);
     }, function(e) {
       console.log("Error" + e.message);
     }, "rw"
 );

see, below FileSystem Tutorial and API Reference

FileSystem Tutorial https://developer.tizen.org/development/tutorials/web-application/tizen-features/base/filesystem#retrieve

Filesystem API Reference https://developer.tizen.org/dev-guide/latest/org.tizen.web.apireference/html/device_api/mobile/tizen/filesystem.html#FileSystemManager::resolve

 

If you put your text file on /project_root/data/text/x.txt. You can access to that file with "wgt-package/data/text/x.txt" path on webapi.

So below is simple example code. try it

 

 

 

 

dwarak besant

You have not shown your currently working code, so it is difficult to determine your exact problem. May be you are missing a privilege? tizen.filesystem.resolve requires http://tizen.org/privilege/filesystem.read, you have to add it to your app config.

Anyhow, with data/text/helloworld.txt on my project folder, following sample code is working just fine:

var textFolder = "wgt-package/data/text";
var helloWorld = "helloworld.txt";

function onsuccess(files) {
    for (var i = 0; i < files.length; i++) {
        if (files[i].name == helloWorld) {
            files[i].openStream("r", function(fs) {
                var text = fs.read(files[i].fileSize);
                fs.close();
                console.log("File contents: " + text);
            }, function(e) {
                console.log("Error " + e.message);
            }, "UTF-8");
            break;
        }
    }
}

    function onerror(error) {
    console.log("The error " + error.message
            + " occurred when listing the files in " + textFolder);
}

tizen.filesystem.resolve(textFolder, function(dir) {
        dir.listFiles(onsuccess, onerror);
    }, function(e) {
        console.log("Error" + e.message);
    }, "r"); // make sure to use 'r' mode as 'wgt-package' is read-only folder

You should see a similar log in JS console as follows:

js/main.js (10) :File contents: Hello World!