PhoneGapHow to create and write data to a file in phonegap

How to create and write data to a file in phonegap

In this tutorial we will learn how to create and write data to a file in phonegap where we can create our own notes and save it in the sdcard .

  • To learn how to create and write data to a file in phonegap follow the steps given below :

  1. First of all download and install Xampp server and gitbash in your machine .
  2. Go to Xampp -> htdocs folder .
  3. In this htdocs folder create a new folder called as Note .
  4. Now in Note folder create a new html file called as index.html and open it with Notepad++ or any other text editor and write the given code in it :
  5. <!DOCTYPE html>
    <html>
        <head>
            <meta name="viewport" content="user-scalable=no,initial-scale=1, 
    maximum-scale=1,minimum-scale=1, width=device-width" />
            <title>PhoneGap App</title>
     <script src="js/jquery-1.10.2.min.js"></script>   <!-jquery-1.10.2.min is the name of my file>
            <script type="text/javascript" src="cordova.js"></script>
            <script type="text/javascript">
    
                var fileObject;
                document.addEventListener("deviceready", onDeviceReady, true);
                function onDeviceReady() 
               {
                  window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,onFileSystemSuccess, fail);
                }
                
                function onFileSystemSuccess(fileSystem) {
                    fileSystem.root.getFile("readme.txt",
                    {create: true, exclusive: false},
                    gotFileEntry, fail);
                  }
             	function gotFileEntry(fileEntry) {
                    fileObject = fileEntry;
                    $('#saveFile_btn').on('click', function() {
                        saveFileContent();
                    });
                }
    
    function saveFileContent() {
                    fileObject.createWriter(gotFileWriter, fail);
                }
                
                function gotFileWriter(writer) {
                    var myText = document.getElementById('my_text').value;
                    writer.write(myText);
                    writer.onwriteend = function(evt) {
                    $('#message').html('<p>File contents have been written.<br /><strong>File path:</strong> ' + fileObject.fullPath + '</p>');
                        var reader = new FileReader();
                        reader.readAsText(fileObject);
                        reader.onload = function(evt) {
                            $('#contents').html('<strong>File contents:</strong> <br />'
                                + evt.target.result);
                        };
                    };
                }
    
    
    function fail(error)
     {
                  	  alert(error.code);
                }
                </script>
        </head>
        <body>
            	<input type="text" id="my_text" />
            	<input type="button" id="saveFile_btn"
                   value="Save" />
            	<div id="message"></div>
            	<div id="contents"></div>
        </body>
    </html>
    

  6. Here are the screen shots for the code :
  7. How to create and write data to a file in phonegap
    Phonegap
    How to create and write data to a file in phonegap
    Create and write data
    write data to a file

  8. Now go to jquery.com and click on the link ” Download the compressed product…..” as shown below by an arrow :
  9. Jquery

  10. After clicking on the link you will be navigated to the next window as shown below :
  11. Write data to a file

  12. So now in your Note folder create a new folder called as js and in this js folder create a new file called as jquery.js and copy paste all the above jquery code shown in the above image in jquery.js file.
  13. Thus now in Note folder you will have the following contents :
  14. Note folder

  15. Now go to Xampp control pannel and start the apache and Mysql modules .
  16. Now enter the following url localhost/Note in your browser and you will have the following output :
  17. how to create and write data to a file in phonegap

  18. Now go through all the given steps to create an apk file of our application .
  19. Hence after writing the code save your document and now sign in into your Github account by visiting github.com :
  20. 0

  21. So now enter a username ,your email id and password in the respective tabs and click on the Sign up for GitHub button .
  22. After you click on the signup button you will get the following window :
  23. 0.1

  24. Now click on the ‘+’ icon on the right hand side as indicated by the red arrow and then click on the “ New repository ” as indicated by the blue arrow .
  25. 0.2

  26. After clicking on the New repository you will be navigated to the next window ,specify a Repository name ,select a public radio button and click on the Create repository button as shown in the image below :
  27. 0.3

  28. So after clicking on the Create repository button you have the following window :
  29. 0.4

  30. Go to xampp -> htdocs .
  31. In htdocs you have your Note folder created .
  32. Now go to your start menu -> GitBash -> right click on it -> Send To -> Desktop (create a shortcut) of GitBash on Desktop as shown below :
  33. 0.7

  34. So now on Desktop you will have a GitBash icon .
  35. Now Right Click on the GitBash icon on the desktop and go to properties and in the Start in edit box set the path as : c:xampphtdocs as shown in the image.
  36. 0.8

  37. Now Click on Apply and OK button .
  38. Now start the GitBash application from the desktop and you will have the following screen :
  39. 0.9

  40. Now insert the following command as shown below in the gitbash command prompt window..
  41. ls
    cd Note(that is your folder name in htdocs folder)
    ls
    

  42. Thus after inserting the commands you will have the following view of your window :
  43. 0.10

  44. After you get this , insert further commands in the same command prompt window as given below :
  45. git config --global user.name “username of your github account”
    
    git config --global user.email “email of your github account”
    
    git config --list
    

  46. After inserting this command your gitbash command prompt window will have the following look , check whether your username and email you entered has been generated in the list :
  47. 0.11

  48. After this insert the following commands :
  49. git init
    
    ls –a
    
    git add .
    
    git commit –am ‘initial commit’
    

  50. You will have the following window :
  51. 0.12

  52. Now go to your github account where you had created the Note repository and copy the add origin command and push command and paste it in the gitbash command prompt respectively.
  53. 0.13

  54. So after adding the commands you will be asked for your github username and password ,so you will have the following window :
  55. 0.14

  56. Thus now the push operation is complete, so now we are ready to build our application .
  57. Now go to www.build.phonegap.com and click on Sign in tab present at the right.
  58. 1

  59. After clicking on the Sign in button you will get the next page where you have to click on the Sign in with GitHub button as shown by the arrow head ..
  60. 2

  61. After that you will be navigated to the next page where you have to click on the new app button as shown by the red arrow head.
  62. 3

  63. After clicking on the new app button ,you will get the following window where you have to select the open-source tab and click on the dropdown pointer and select your app as shown in the image :
  64. 4

    NOTE : Here you will have your own application name that is the name given by you while creating the repository.

  65. When you select your app the fetching process will start as shown in the figure :
  66. 5

  67. After the fetching is complete you will get the following window :
  68. 6

  69. Now click on the Ready to Build button as shown by the red arrow in the image and you will get the following screen :
  70. 7

  71. Click on the android icon and you will have the following window popping up to save the apk file.
  72. 8

  73. Now save your apk file and insert it in your android mobile phone and install it ,you will have the application running on your mobile phone
  74. Thus we have successfully learnt how to create and write data to a file in phonegap .

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -