Save File Internal Storage Android
Storage quickview
- Android Internal Storage Files
- Android Save File To Internal Storage Public
- Save File In Internal Storage Android Programmatically
- Use Shared Preferences for primitive data
- Use internal device storage for private data
- Use external storage for large data sets that are not private
- Use SQLite databases for structured storage
In this document
Android Internal storage is the storage of the private data on the device memory. By default, saving and loading files to the internal storage are private to the application and other applications will not have access to these files. Internal Storage. Files are accessible by only your app; Files are removed when your app is uninstalled; Files are always available (meaning they files will never be saved on a removable memory) External Storage. Files are fully readable by other apps (including any variant of File Manager app, in your case).
See also
Android provides several options for you to save persistent application data. The solution youchoose depends on your specific needs, such as whether the data should be private to yourapplication or accessible to other applications (and the user) and how much space your datarequires.
Your data storage options are the following:
- Shared Preferences
- Store private primitive data in key-value pairs.
- Internal Storage
- Store private data on the device memory.
- External Storage
- Store public data on the shared external storage.
- SQLite Databases
- Store structured data in a private database.
- Network Connection
- Store data on the web with your own network server.
Android provides a way for you to expose even your private data to other applications— with a contentprovider. A content provider is an optional component that exposes read/write access toyour application data, subject to whatever restrictions you want to impose. For more informationabout using content providers, see theContent Providersdocumentation.
Using Shared Preferences
The SharedPreferences
class provides a general framework that allows youto save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences
to save any primitive data: booleans, floats, ints, longs, andstrings. This data will persist across user sessions (even if your application is killed).
User Preferences
Shared preferences are not strictly for saving 'user preferences,' such as what ringtone auser has chosen. If you're interested in creating user preferences for your application, see PreferenceActivity
, which provides an Activity framework for you to createuser preferences, which will be automatically persisted (using shared preferences).
To get a SharedPreferences
object for your application, use one oftwo methods:
getSharedPreferences()
- Use this if you need multiple preferences files identified by name,which you specify with the first parameter.getPreferences()
- Use this if you needonly one preferences file for your Activity. Because this will be the only preferences filefor your Activity, you don't supply a name.
To write values:
- Call
edit()
to get aSharedPreferences.Editor
. - Add values with methods such as
putBoolean()
andputString()
. - Commit the new values with
commit()
To read values, use SharedPreferences
methods such as getBoolean()
and getString()
.
Here is an example that saves a preference for silent keypress mode in acalculator:
Using the Internal Storage
You can save files directly on the device's internal storage. By default, files savedto the internal storage are private to your application and other applications cannot accessthem (nor can the user). When the user uninstalls your application, these files are removed.
To create and write a private file to the internal storage:
- Call
openFileOutput()
with thename of the file and the operating mode. This returns aFileOutputStream
. - Write to the file with
write()
. - Close the stream with
close()
.
For example:
MODE_PRIVATE
will create the file (or replace a file ofthe same name) and make it private to your application. Other modes available are: MODE_APPEND
, MODE_WORLD_READABLE
, and MODE_WORLD_WRITEABLE
.
To read a file from internal storage:
- Call
openFileInput()
and pass it thename of the file to read. This returns aFileInputStream
. - Read bytes from the file with
read()
. - Then close the stream with
close()
.
Tip: If you want to save a static file in your application atcompile time, save the file in your project res/raw/
directory. You can open it withopenRawResource()
, passing the R.raw.<filename>
resource ID. This method returns an InputStream
that you can use to read the file (but you cannot write to the original file).
Saving cache files
If you'd like to cache some data, rather than store it persistently, you should use getCacheDir()
to open a File
that represents the internal directory where your application should savetemporary cache files.
When the device islow on internal storage space, Android may delete these cache files to recover space. However, youshould not rely on the system to clean up these files for you. You should always maintain the cachefiles yourself and stay within a reasonable limit of space consumed, such as 1MB. When the useruninstalls your application, these files are removed.
Other useful methods
getFilesDir()
- Gets the absolute path to the filesystem directory where your internal files are saved.
getDir()
- Creates (or opens an existing) directory within your internal storage space.
deleteFile()
- Deletes a file saved on the internal storage.
fileList()
- Returns an array of files currently saved by your application.
Using the External Storage
Every Android-compatible device supports a shared 'external storage' that you can use tosave files. This can be a removable storage media (such as an SD card) or an internal(non-removable) storage. Files saved to the external storage are world-readable and canbe modified by the user when they enable USB mass storage to transfer files on a computer.
It's possible that a device using a partition of theinternal storage for the external storage may also offer an SD card slot. In this case,the SD card is not part of the external storage and your app cannot access it (the extrastorage is intended only for user-provided media that the system scans).
Caution: External storage can become unavailable if the user mounts theexternal storage on a computer or removes the media, and there's no security enforced upon files yousave to the external storage. All applications can read and write files placed on the externalstorage and the user can remove them.
Checking media availability
Before you do any work with the external storage, you should always call getExternalStorageState()
to check whether the media is available. Themedia might be mounted to a computer, missing, read-only, or in some other state. For example,here's how you can check the availability:
This example checks whether the external storage is available to read and write. ThegetExternalStorageState()
method returns other states that youmight want to check, such as whether the media is being shared (connected to a computer), is missingentirely, has been removed badly, etc. You can use these to notify the user with more informationwhen your application needs to access the media.
Accessing files on external storage
If you're using API Level 8 or greater, use getExternalFilesDir()
to open a File
that represents the external storage directory where you should save yourfiles. This method takes a type
parameter that specifies the type of subdirectory youwant, such as DIRECTORY_MUSIC
andDIRECTORY_RINGTONES
(pass null
to receivethe root of your application's file directory). This method will create theappropriate directory if necessary. By specifying the type of directory, youensure that the Android's media scanner will properly categorize your files in the system (forexample, ringtones are identified as ringtones and not music). If the user uninstalls yourapplication, this directory and all its contents will be deleted.
If you're using API Level 7 or lower, use getExternalStorageDirectory()
, to open a File
representing the root of the external storage. You should then write your data in thefollowing directory:
The <package_name>
is your Java-style package name, such as 'com.example.android.app
'. If the user's device is running API Level 8 or greater and theyuninstall your application, this directory and all its contents will be deleted.
Hiding your files from the Media Scanner
Include an empty file named .nomedia
in your external files directory (note the dotprefix in the filename). This will prevent Android's media scanner from reading your mediafiles and including them in apps like Gallery or Music.
Saving files that should be shared
If you want to save files that are not specific to your application and that should notbe deleted when your application is uninstalled, save them to one of the public directories on theexternal storage. These directories lay at the root of the external storage, such as Music/
, Pictures/
, Ringtones/
, and others.
In API Level 8 or greater, use getExternalStoragePublicDirectory()
, passing it the type of public directory you want, such asDIRECTORY_MUSIC
, DIRECTORY_PICTURES
,DIRECTORY_RINGTONES
, or others. This method will create theappropriate directory if necessary.
If you're using API Level 7 or lower, use getExternalStorageDirectory()
to open a File
that representsthe root of the external storage, then save your shared files in one of the followingdirectories:
Music/
- Media scanner classifies all media found here as user music.Podcasts/
- Media scanner classifies all media found here as a podcast.Ringtones/
- Media scanner classifies all media found here as a ringtone.Alarms/
- Media scanner classifies all media found here as an alarm sound.Notifications/
- Media scanner classifies all media found here as a notificationsound.Pictures/
- All photos (excluding those taken with the camera).Movies/
- All movies (excluding those taken with the camcorder).Download/
- Miscellaneous downloads.
Saving cache files
If you're using API Level 8 or greater, use getExternalCacheDir()
to open a File
that represents theexternal storage directory where you should save cache files. If the user uninstalls yourapplication, these files will be automatically deleted. However, during the life of yourapplication, you should manage these cache files and remove those that aren't needed in order topreserve file space.
If you're using API Level 7 or lower, use getExternalStorageDirectory()
to open a File
that representsthe root of the external storage, then write your cache data in the following directory:
The <package_name>
is your Java-style package name, such as 'com.example.android.app
'.
Using Databases
Android provides full support for SQLite databases.Any databases you create will be accessible by name to anyclass in the application, but not outside the application.
The recommended method to create a new SQLite database is to create a subclass of SQLiteOpenHelper
and override the onCreate()
method, in which youcan execute a SQLite command to create tables in the database. For example:
You can then get an instance of your SQLiteOpenHelper
implementation using the constructor you've defined. To write to and read from the database, callgetWritableDatabase()
and getReadableDatabase()
, respectively. These both return aSQLiteDatabase
object that represents the database andprovides methods for SQLite operations.
Android does not impose any limitations beyond the standard SQLite concepts. We do recommendincluding an autoincrement value key field that can be used as a unique ID toquickly find a record. This is not required for private data, but if youimplement a content provider,you must include a unique ID using the BaseColumns._ID
constant.
You can execute SQLite queries using the SQLiteDatabase
query()
methods, which accept various query parameters, such as the table to query,the projection, selection, columns, grouping, and others. For complex queries, such asthose that require column aliases, you should useSQLiteQueryBuilder
, which providesseveral convienent methods for building queries.
Every SQLite query will return a Cursor
that points to all the rowsfound by the query. The Cursor
is always the mechanism with whichyou can navigate results from a database query and read rows and columns.
For sample apps that demonstrate how to use SQLite databases in Android, see theNote Pad andSearchable Dictionaryapplications.
Database debugging
The Android SDK includes a sqlite3
database tool that allows you to browsetable contents, run SQL commands, and perform other useful functions on SQLitedatabases. See Examining sqlite3databases from a remote shell to learn how to run this tool.
Using a Network Connection
Android Internal Storage Files
You can use the network (when it's available) to store and retrieve data on your own web-basedservices. To do network operations, use classes in the following packages:
If you have your files stored on the internal memory of your device, the following guide should teach you how you can access the internal storage on your Android device. It teaches multiple methods of doing the task so read on to learn all the available methods.
Android Manage & Backup Tips
Android File Management
Android Backup Tips
Android Cleanup Tips
Every Android device that you buy has an internal memory on it called internal storage. This memory space is used to store the essential files of your device such as the camera folders, application data, and so on. If you have not added an SD card to your device, you have already been using the internal storage of your device all this time whether you knew it or you did not.
There are also some methods available to let you access the internal storage of your Android device as if you were viewing something in a file manager. Your internal storage can be mounted as a drive on your computer and then you can access its data as though you were accessing any local folder on your machine.
The following guide covers two of the best ways to access the files stored on your internal memory space. Let’s check them out.
2 Methods to Access Android Internal Storage
As mentioned above, this section is going to walk you through two methods that will help you view the files saved on your phone’s internal storage. Both methods should help you reach the same set of files on your device.
Method 1. Access Android Internal Storage via USB Cable
If you would like to use the traditional USB method of transferring files, you can use a USB cable with your Android device to access your internal storage. You are going to connect your device to your computer with a USB cable to then be able to view your files.
The following are the exact steps on how you can do the task on your computer:
Step 1: If you use a Mac computer, you will have to install and use the Android File Transfer app to access the internal storage of your device on your Mac. Windows users do not need to do this step.
Step 2: Connect your Android device to your computer using a compatible USB cable. Tap on the notification that appears on your device and choose File transfer.
Step 3: Open This PC (Windows) or the Android File Transfer app (Mac) and you should be able to access the internal files of your device. You can then move around your files however you want.
Access internal storage files on a computer
As you can see, it is pretty easy to mount your Android device as a drive on your computer and access its files. Keep in mind, though, that not all the devices can be mounted that way as sometimes you will run into issues like your computer lacking the required drivers to recognize your device.
If you would like to eliminate all those issues that may possibly occur, you can use an app to view your internal storage as described in the following section.
Method 2. Access Android Internal Storage via AnDroid
If you do not wish to get into the hassle of finding and installing the required drivers for your device on your computer, you can use a third-party app to view your internal memory files without installing anything but the app.
Enter AnyDroid, the application that allows you to access your internal storage on your Android device with minimum hassle. All you need to do is to connect your device to computer and the app will take care of the rest for you. It automatically recognizes your device and lets you perform a number of tasks on it.
Here are some of the useful features the app offers to its users:
- Full access– It gives you full access to each and every file stored on your internal storage.
- Transfer multiple files – the app allows you to transfer any type of files from your device to your computer and vice versa.
- Safe– it is very safe and secure to use the app to access and transfer your files.
- Easy– whether you are an expert or a novice, you will equally enjoy using AnyDroid on your computer.
AnyDroid has everything you would ever need to manage all kinds of files on your Android device’s internal storage. The following is how you use the app to access your internal storage.
Step 1: Go download and install the app on your computer. Launch the app and plug-in your Android device to your computer using a cable.
Free Download * 100% Clean & Safe
Plug-in Android device to a computer
Step 2: The main screen of the app will appear. Find and click on the option that says Files and it will let you access the files saved on your internal storage.
Access the Files option to view files on Android internal storage
Step 3: Once you have clicked the files option, the app will display all the files and folders you have got on your device. These are all the files and folders available on your device’s internal storage.
Step 4: If you want to move a file or a folder to your computer, select the file or the folder and click on the Send to PC button at the top to transfer your content to computer.
Transfer files from internal storage to computer
Step 5: Wait while the app transfers your content. When it is done, you will see the following message on your screen.
Files successfully transferred from internal storage to computer
Using AnyDroid to access the files on the internal storage of an Android device is extremely easy and it allows file transfer tasks as well should you want to do that.
Android Save File To Internal Storage Public
The Bottom Line
If you have never accessed the internal storage files on your Android device, the above guide should help you do that so you can see what you have got on your internal memory space. We hope the guide helps access the file you really wanted to view on your device.
Save File In Internal Storage Android Programmatically
Product-related questions? Contact Our Support Team to Get Quick Solution >