简体   繁体   中英

Saving data in tizen web app

I wrote a app and I need to save its data in local tizen mobile. This is the code that I wrote:

    <script type="text/javascript">
        function saveData() {

            var a = document.getElementById('star');
            var b = document.getElementById('checkone');
            var c = document.getElementById('note');
            var d = document.getElementById('med');
            var e = document.getElementById('checktwo');
            var f = document.getElementById('wgt');
            var g = document.getElementById('wgt1');

            /* localStorage.getItem("a");
            localStorage.getItem("b");
            localStorage.getItem("c");
            localStorage.getItem("d");
            localStorage.getItem("e");
            localStorage.getItem("f");
            localStorage.getItem("g"); */

            /* Set the local storage item */
            if ("localStorage" in window) {
                localStorage.setItem(a, star);
                localStorage.setItem(b, checkone);
                localStorage.setItem(c, note);
                localStorage.setItem(d, med);
                localStorage.setItem(e, checktwo);
                localStorage.setItem(f, wgt);
                localStorage.setItem(g, wgt1);
                location.reload();
                alert("Date got")
            } else {
                alert("no localStorage in window");
            }

            /* Set the session storage item */
            if ("sessionStorage" in window) {
                sessionStorage.setItem(a, star);
                sessionStorage.setItem(b, checkone);
                sessionStorage.setItem(c, note);
                sessionStorage.setItem(d, med);
                sessionStorage.setItem(e, checktwo);
                sessionStorage.setItem(f, wgt);
                sessionStorage.setItem(g, wgt1);
                location.reload();
                alert("Data has been set")
            } else {
                alert("no sessionStorage in window");
            }

            window.onload = function() {
                var localhtml = "";
                var sessionhtml = "";

                /* Get the local storage item */
                for (var i = 0; i < localStorage.length; i++) {
                    localhtml += "<li>" + localStorage.key(i) + " : "
                            + localStorage.getItem(localStorage.key(i))
                            + "</li>";
                }
                document.getElementById("localStorageData").innerHTML = localhtml;

                /* Get the session storage item */
                for (var j = 0; j < sessionStorage.length; j++) {
                    sessionhtml += "<li>" + sessionStorage.key(j) + " : "
                            + sessionStorage.getItem(sessionStorage.key(j))
                            + "</li>";
                }
                document.getElementById("sessionStorageData").innerHTML = sessionhtml; 
            }
            tizen.filesystem.resolve('/opt/apps/trial/data', function(
                    dir) {
                documentsDir = dir;
                dir.listFiles(onsuccess, onerror);
            }, function(e) {
                console.log("Error" + e.message);
            }, "rw");

        }
    </script>

In this part:

tizen.filesystem.resolve('/opt/apps/trial/data', function(
                        dir) {
                    documentsDir = dir;
                    dir.listFiles(onsuccess, onerror);
                }, function(e) {
                    console.log("Error" + e.message);
                }, "rw");

            }

trial is the app Id, I'm not understanding about how to get the app Id from tizen phone. And data gets stored successfully, but I'm unable to retrieve the stored data.

Starting from the end of your question.

To get the Application ID (and Package ID) you may use the tizen.application API

tizen.application.getCurrentApplication().appInfo.id // returns App Id
tizen.application.getCurrentApplication().appInfo.packageId // returns packageId

Other props like: .name , .version , .iconPath etc. can be found there as well.

File access - you may resolve paths using locations relative to a virtual path ( documents , images , music ,...) or use a file:/// prefix for absolute paths.

Please see the Filesystem API inside SDK docs for reference.

You may also verify that you have the proper privileges defined:

http://tizen.org/privilege/filesystem.read
http://tizen.org/privilege/filesystem.write

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM