| Start | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Android: Internal and External Storage - Terminology Clarification Android developers(mainly beginners) have been confused regarding Internal & External storage terminology. There are lot of questions on Stackoverflow regarding the same. This is mainly because of the fact that terminology according to Google/official Android documentation is quite different to that of normal Android OS user. Hence I thought documenting this would help. What we think - User’s Terminology (UT)
But, According to Android Documentation/Guide - Google’s Terminology (GT) Internal storage(GT): By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). External storage(GT): This can be a removable storage media (such as an SD card) or an internal (non-removable) storage. External Storage(GT) can be categorized into two types:
In a nutshell, External Storage(GT) = Internal Storage(UT) and External Storage(UT) Removable Storage(GT) = External Storage(UT) Internal Storage(GT) doesn't have a term in UT. Let me explain clearly, Internal Storage(GT): By default, files saved to the internal storage are private to your application and other applications cannot access them. Your app user also can't access them using file manager; even after enabling "show hidden files" option in file manager. To access files in Internal Storage(GT), you have to root your Android phone. Moreover, when the user uninstalls your application, these files are removed/deleted. So Internal Storage(GT) is NOT what we think as Nexus 6P's 32/64 GB internal memory Generally, Internal Storage(GT) location would be: External Storage(GT): Every Android-compatible device supports a shared "external storage" that you can use to save files. Files saved to the external storage are world-readable and can be modified by the user when they enable USB mass storage to transfer files on a computer. External Storage(GT) location: It could be anywhere in your internal storage(UT) or in your removable storage(GT) i.e. micro SD card. It depends on your phone's OEM and also on Android OS version. In order to read or write files on the External Storage(GT), your app must acquire the For example: If you need to both read and write files, then you need to request only the `WRITE_EXTERNAL_STORAGE` permission, because it implicitly requires read access as well. In External Storage(GT), you may also save files that are app-private But, When the user uninstalls your application, this directory and all its contents are deleted. When do you need to save files that are app-private in External Storage(GT)? If you are handling files that are not intended for other apps to use (such as graphic textures or sound effects used by only your app), you should use a private storage directory on the external storage Beginning with Android 4.4, reading or writing files in your app's private directories does not require the Methods to store in Internal Storage(GT): Both these methods are present in Context (opens new window) class Methods to store in Primary External Storage i.e. Internal Storage(UT): In the beginning, everyone used Environment.getExternalStorageDirectory() (opens new window) , which pointed to the root of Primary External Storage. As a result, Primary External Storage was filled with random content. Later, these two methods were added: Methods to store in Removable Storage(GT) i.e. micro SD card Before API level 19, there was no official way to store in SD card. But, many could do it using unofficial libraries or APIs. Officially, one method was introduced in It returns absolute paths to application-specific directories on all shared/external storage devices where the application can place persistent files it owns. These files are internal to the application, and not typically visible to the user as media. That means, it will return paths to both types of External Storage(GT) - Internal memory and Micro SD card. Generally second path would be storage path of micro SD card(but not always). So you need to check it out by executing the code with this method. Example with code snippet: I created a new android project with empty activity, wrote the following code inside
After executing above code, Output: Note: I have connected my phone to Windows PC; enabled both developer options, USB debugging and then ran this code. If you do not connect your phone; but instead run this on Android emulator, your output may vary. My phone model is Coolpad Note 3 - running on Android 5.1 Storage locations on my phone: Micro SD storage location: Internal Storage(UT) location: Note that To clearly understand different storage paths in Android, Please go through this answer (opens new window) Disclaimer: All the storage paths mentioned above are paths on my phone. Your files may not be stored on same storage paths. Because, the storage locations/paths may vary on other mobile phones depending on your vendor, manufacturer and different versions of Android OS. |
||||||||||||||
| Using External Storage "External" Storage is another type of storage that we can use to save files to the user's device. It has some key differences from "Internal" Storage, namely:
To use External Storage, we need to first obtain the proper permissions. You will need to use:
To grant these permissions, you will need to identify them in your NOTE: Since they are Dangerous permissions (opens new window) if you are using API Level 23 or above, you will need to request the permissions at runtime (opens new window). Before attempting to write or read from External Storage, you should always check that the storage medium is available. When writing files to the External Storage, you should decide if the file should be recognized as Public or Private. While both of these types of files are still accessible to the user and other applications on the device, there is a key distinction between them. Public files should remain on the device when the user uninstalls the app. An example of a file that should be saved as Public would be photos that are taken through your application. Private files should all be removed when the user uninstalls the app. These types of files would be app specific, and not be of use to the user or other applications. Ex. temporary files downloaded/used by your application. Here's how to get access to the Public Private |
||||||||||||||
| Using Internal Storage By default, any files that you save to Internal Storage are private to your application. They cannot be accessed by other applications, nor the user under normal circumstances. These files are deleted when the user uninstalls the application. To Write Text to a File To Append Text to an Existing File Use |
||||||||||||||
Save Database on SD Card (Backup DB on SD) call this method this way : ExportDB("myDB.db","com.example.exam","/myFolder"); |
||||||||||||||
| Fetch Device Directory : First Add Storage permission to read/fetch device directory. Create model class Create list using directory model to add directory data. Fetch directory using following method. Print directory list in log. Usage To fetch inner files/folder of specific directory use same method just change argument, pass the current selected path in argument and handle response for same. To get File Extension : Syntax
Parameters
|
Read Article
https://develop.horje.com/learn/1434/reference