

{"id":77780,"date":"2020-05-11T18:13:09","date_gmt":"2020-05-11T12:43:09","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=77780"},"modified":"2024-08-27T13:25:14","modified_gmt":"2024-08-27T07:55:14","slug":"storage-in-android","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/storage-in-android\/","title":{"rendered":"Storage in Android &#8211; How to save data and files in Android?"},"content":{"rendered":"<p>In this Android tutorial, we\u2019ll learn about<strong> Storage in Android.<\/strong>\u00a0We will see what are the different ways to save the application data in android, what is internal storage in android and what is external storage in android? Then we will learn how to save files in Android. So let&#8217;s begin our journey.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Storage-in-Android.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77856\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Storage-in-Android.jpg\" alt=\"Storage in Android\" width=\"802\" height=\"420\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Storage-in-Android.jpg 802w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Storage-in-Android-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Storage-in-Android-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Storage-in-Android-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Storage-in-Android-520x272.jpg 520w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/a><\/p>\n<h2>Storage in Android<\/h2>\n<p>Android uses a file system that is like a disk-based file system of other platforms. The Android system provides different options to save application data:<\/p>\n<ul>\n<li>Application-specific storage in Android<\/li>\n<li>Shared storage in Android<\/li>\n<li>Preferences in Android<\/li>\n<li>Databases in Android<\/li>\n<\/ul>\n<p>Let us go through these all one by one.<\/p>\n<h3>1. Android Application-specific storage<\/h3>\n<p>In this, the stored data is meant only for a specific application\u2019s use. It is stored either in a dedicated directory inside the internal storage or in external storage. The sensitive data, that is specific to the app and shall not be accessible to other apps is generally stored in Internal Storage. There are certain things about it that are:<\/p>\n<p><strong>a. To access it, we have two ways:<\/strong><\/p>\n<ul>\n<li>Internal Storage<br \/>\n<strong>getFilesDir() or getCacheDir()<\/strong><\/li>\n<li>External Storage<br \/>\n<strong>getExternalFilesDir() or getExternalCacherDir()<\/strong><\/li>\n<\/ul>\n<p>b. Permission is not required for Internal Storage as well as for External Storage. When considering external storage, we do not need permission for Android version 4.4(API level 19) or higher.<\/p>\n<p>c. If the app data is in internal storage, other apps cannot access it. If the data is in external storage, files can be accessed by other apps.<\/p>\n<p>d. If the application is uninstalled by any means, the data files also get deleted.<\/p>\n<h3>2. Shared Storage in Android<\/h3>\n<p>In this, the stored data is meant to be shared among other apps as well. It includes data such as multimedia, documents, etc. There are certain things about it that are:<\/p>\n<p>a. Access the data with MediaStore API and Storage Access Framework.<\/p>\n<p>b. To read the media file use these permissions: <strong>READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE<\/strong> for Android 10 or higher.<\/p>\n<p>c. You can access the media files by other files through <strong>READ_EXTERNAL_STORAGE<\/strong> and the document files, through system file picker.<\/p>\n<p>d. If you uninstall the app, the data would not be deleted from the External Storage.<\/p>\n<h3>3. Android Preferences<\/h3>\n<p>They store the data in a private database. It stores the data in the form of a key-value pair. There are certain things about it that are:<\/p>\n<p>a. Data is stored in a key-value pair.<br \/>\nb. It can be accessed through the Jetpack preference library.<br \/>\nc. Data from this cannot be accessed through other applications.<br \/>\nd. When you uninstall the app, the data gets deleted.<\/p>\n<h3>4. Android Databases<\/h3>\n<p>In this, the data is stored as structured data in a private database. For that, it uses the Room persistence library. There are certain things about it that are:<\/p>\n<p>a. It has structured data<br \/>\nb. To access the data use Room persistence library<br \/>\nc. No other application can access the data<br \/>\nd. On the uninstallation of the application, the data gets deleted too.<\/p>\n<h2>Internal Storage in Android<\/h2>\n<p>Files can be saved directly in the \u201cinternal\u201d storage of the device. When the files are saved in the internal storage, they are automatically set to private. These files are set to private so they cannot be used by other applications.<\/p>\n<p>Internal storage is ideal for storing data which can not be accessible to any other applications like .config files,etc. Also in case the app gets uninstalled the files are removed so it helps in memory management on device. Unlike external storage, internal storage does not need any type of special permissions to perform operations on the file.<\/p>\n<p>We can <strong>create and write a private file in the internal storage<\/strong> as follows:<\/p>\n<p>a. Invoke the method, callFileOutput() with the name of the file and the correct mode. It\u2019ll return the FileOutputStream.<br \/>\nb. Write in the file, using the write() method.<br \/>\nc. After writing in the file, close it using the close() method.<\/p>\n<p>We can <strong>read from a file from internal storage<\/strong> as follows:<\/p>\n<p>a. Invoke the method, callFileOutput() with the name of the file and the correct mode. It\u2019ll return the FileInputStream.<br \/>\nb. Read the file using the read() method.<br \/>\nc. After reading, close the stream using the close() method.<\/p>\n<p><strong>For example:<\/strong><\/p>\n<p><strong>To read &#8211;<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">FileOutputStream fOut = openFileOutput(\u201c file_name \u201d,MODE_INTERNAL_READ);\r\nString name= \u201cJohn\u201d;\r\nfOut.write(name.getBytes());\r\nfOut.close()<\/pre>\n<p><strong>To write &#8211;<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">FileInputStream fIn = openFileInput(file_name);\r\nInt x;\r\nString data= \u201c\u201d; \/\/ it will hold the data\r\nif( (x.fIn.read()) ! = -1){\r\ndata = data + Character.toString((char)ch);\r\n}\r\nfOut.close()<\/pre>\n<p>There are certainly some other important methods, which are as mentioned below:<\/p>\n<ul>\n<li><strong>getFileDir()-<\/strong> It helps to get the absolute path to the directory where internal files are there.<\/li>\n<li><strong>getDir()-<\/strong> It opens an existing directory in the internal storage space. It also creates one, if it doesn\u2019t exist.<\/li>\n<li><strong>deleteFile()-<\/strong> It deletes an existing file that was there on the internal storage.<\/li>\n<li><strong>fileList()-<\/strong> This method returns an array of the files currently saved by the application<\/li>\n<li><strong>write (byte[] buffer, int byteOffset, int byteCount)-<\/strong> It writes count bytes from the byte array buffer that starts from the position offset to the stream. It is for FileInputStream.<\/li>\n<li><strong>read (byte[] buffer, int byteOffset, int byteCount)-<\/strong> It reads the length bytes and stores them in the byte array that starts at the offset. It is for FileOutputStream.<\/li>\n<\/ul>\n<h2>External Storage in Android<\/h2>\n<p>You can store Files in the \u201cexternal storage\u201d of the device as well. External storage of a device can be a removable storage media(memory cards), or internal non-removable storage (such as RAM). When we store a file in the external file, it becomes accessible through other applications as well. These files do not get deleted if we uninstall the Application.<\/p>\n<p>External storage allows apps to share files that users can direct easily like videos, music, etc. Files will be retained on the device even if an app is uninstalled which leaves useless files eating up memory. External storage needs permissions to perform operations on the file.<\/p>\n<p><strong>Accessing the files on the External Storage:<\/strong><\/p>\n<h3>a. How to Open a file in Android?<\/h3>\n<p>To open a file that is in the external file, use the <strong>getExternalFilesDir()<\/strong> method. It\u2019ll take a type parameter to specify the type of subdirectory that we need. The types can be such as DIRECTORY_ALARMS or DIRECTORY_MUSIC, etc. Pass null to receive the root of the application\u2019s directory.<\/p>\n<p>To open a file that represents the root of the external storage in the API level 7 or lower, you can use<strong> getExternalStorageDirectory()<\/strong>. If you want to write some data, then write the data in the following directory:<br \/>\n<strong>\/Android\/data\/your package\/files\/<\/strong><\/p>\n<p>Here, your package need to be mentioned, like \u201ccom.dataflair.myandroidapp\u201d.<\/p>\n<h3>b. How Save the files in Android?<\/h3>\n<p>To keep the files that are not specific to the application and should not be deleted with the application, you need to save them. You can store them in the directories of the external storage such as Music\/, Movies\/, Download\/, and many more.<\/p>\n<p>To save the file in API level 8 or greater you can use the <strong>getExternalStoragePublicDirectory()<\/strong> method. In this method, you can pass the type of public directory that you want to. You can use public directories such as DIRECTORY_RINGTONES, DIRECTORY_MUSIC, or some other. This can also create a directory if required.<\/p>\n<p>For the API level lower than or equal to 7, use the <strong>getExternalStorageDirectory()<\/strong> method to open a File that represents the root of external storage, and later save the files in any one of the directories that are as below:<\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400\">Music\/<\/span><\/td>\n<td><span style=\"font-weight: 400\">RingTones\/<\/span><\/td>\n<td><span style=\"font-weight: 400\">Alarms\/<\/span><\/td>\n<td><span style=\"font-weight: 400\">Notifications\/<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Pictures\/<\/span><\/td>\n<td><span style=\"font-weight: 400\">Movies\/\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Download\/<\/span><\/td>\n<td><span style=\"font-weight: 400\">Podcasts\/<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Summary<\/h2>\n<p>So, in this article, we have discussed the Storage method for Android. Here we saw what are the different options to store the data and the files for an Android application. We then discussed how we can store the files and data in the Internal and External storage of the device.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Android tutorial, we\u2019ll learn about Storage in Android.\u00a0We will see what are the different ways to save the application data in android, what is internal storage in android and what is external&#46;&#46;&#46;<\/p>\n","protected":false},"author":10,"featured_media":77856,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18722],"tags":[22258,22261,22260,22259],"class_list":["post-77780","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","tag-android-storage","tag-external-storage-in-android","tag-internal-storage-in-android","tag-storage-in-android"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Storage in Android - How to save data and files in Android? - DataFlair<\/title>\n<meta name=\"description\" content=\"Storage in Android - Learn How to save files in Android, various storage options available in android like internal storage, external storage.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/storage-in-android\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Storage in Android - How to save data and files in Android? - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Storage in Android - Learn How to save files in Android, various storage options available in android like internal storage, external storage.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/storage-in-android\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-11T12:43:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-08-27T07:55:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Storage-in-Android.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Storage in Android - How to save data and files in Android? - DataFlair","description":"Storage in Android - Learn How to save files in Android, various storage options available in android like internal storage, external storage.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/storage-in-android\/","og_locale":"en_US","og_type":"article","og_title":"Storage in Android - How to save data and files in Android? - DataFlair","og_description":"Storage in Android - Learn How to save files in Android, various storage options available in android like internal storage, external storage.","og_url":"https:\/\/data-flair.training\/blogs\/storage-in-android\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-05-11T12:43:09+00:00","article_modified_time":"2024-08-27T07:55:14+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Storage-in-Android.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/storage-in-android\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/storage-in-android\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33"},"headline":"Storage in Android &#8211; How to save data and files in Android?","datePublished":"2020-05-11T12:43:09+00:00","dateModified":"2024-08-27T07:55:14+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/storage-in-android\/"},"wordCount":1294,"commentCount":2,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/storage-in-android\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Storage-in-Android.jpg","keywords":["android storage","external storage in android","internal storage in android","storage in android"],"articleSection":["Android Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/storage-in-android\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/storage-in-android\/","url":"https:\/\/data-flair.training\/blogs\/storage-in-android\/","name":"Storage in Android - How to save data and files in Android? - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/storage-in-android\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/storage-in-android\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Storage-in-Android.jpg","datePublished":"2020-05-11T12:43:09+00:00","dateModified":"2024-08-27T07:55:14+00:00","description":"Storage in Android - Learn How to save files in Android, various storage options available in android like internal storage, external storage.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/storage-in-android\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/storage-in-android\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/storage-in-android\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Storage-in-Android.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Storage-in-Android.jpg","width":802,"height":420,"caption":"Storage in Android"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/storage-in-android\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Android Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/android\/"},{"@type":"ListItem","position":3,"name":"Storage in Android &#8211; How to save data and files in Android?"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team is passionate about delivering top-notch tutorials and resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With expertise in the tech industry, we simplify complex topics to help learners excel. Stay updated with our latest insights.","url":"https:\/\/data-flair.training\/blogs\/author\/dfadteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/77780","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=77780"}],"version-history":[{"count":7,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/77780\/revisions"}],"predecessor-version":[{"id":143257,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/77780\/revisions\/143257"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/77856"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=77780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=77780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=77780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}