{"id":1813,"date":"2022-08-30T15:19:39","date_gmt":"2022-08-30T15:19:39","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/how-to-store-data-in-android-duplicate-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:19:39","modified_gmt":"2022-08-30T15:19:39","slug":"how-to-store-data-in-android-duplicate-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-store-data-in-android-duplicate-collection-of-common-programming-errors\/","title":{"rendered":"How to store data in android? [duplicate]-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to store some data which I received from my server into a file in my android project. But I&#8217;m not able to do that.. I used the following code which I&#8217;ve shown below.. Please help me out someone. Thanks.<\/p>\n<pre><code>String FILENAME = \"hello_file\";\nString string = \"hello world!\";\n\nFileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);\nfos.write(string.getBytes());\nfos.close();\n<\/code><\/pre>\n<p>The error its giving is &#8220;Content cannot be resolved to a variable&#8221;. I imported android.content.. Then its giving an error for all the 3 lines called &#8220;Unhandled Exception type FileNotFoundException&#8221; But I have created that file.. The problem is that I&#8217;m not knowing in which folder of the project do I keep that file? Can someone please help me out?<\/p>\n<ol>\n<li>\n<p>Two way of saving a data is allowed in Android that is by using SharedPreference or Sqlite DB File writing is not advisable.<\/p>\n<p>For your Question 1. Manifest Should have write permission.<\/p>\n<p>If not please send the code.. Its very basic<\/p>\n<p>My suggestion is SharedPreference for you.<\/p>\n<\/li>\n<li>\n<p><strong>&#8211;<\/strong> The most common mistake to take place here is that you have <strong>not<\/strong> given the <code>permission to wite external storage<\/code> in <code>AndroidManifest.xml<\/code> file.<\/p>\n<p><strong>&#8211;<\/strong> Please include this into your manifest file<\/p>\n<\/li>\n<li>\n<p>Use<\/p>\n<pre><code>File root = context.getFilesDir();\n\nFile file = new File(root, fileName);\nfos = new FileOutputStream(file);\n<\/code><\/pre>\n<\/li>\n<li>\n<p>1st check the Manifest Permission as suggested by Kumar if still not sorted your problem out then take a look at the following link to get Help<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 01:40:50. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to store some data which I received from my server into a file in my android project. But I&#8217;m not able to do that.. I used the following code which I&#8217;ve shown below.. Please help me out someone. Thanks. String FILENAME = &#8220;hello_file&#8221;; String string = &#8220;hello world!&#8221;; FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1813","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1813","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1813"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1813\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}