What would happen if you tried to open a file created in one app in a different app?
In many cases, your app creates files that other apps don't need to access, or shouldn't access. The system provides the following locations for storing such app-specific files: Show
When the user uninstalls your app, the files saved in app-specific storage are removed. Because of this behavior, you shouldn't use this storage to save anything that the user expects to persist independently of your app. For example, if your app allows users to capture photos, the user would expect that they can access those photos even after they uninstall your app. So you should instead use shared storage to save those types of files to the appropriate media collection. The following sections describe how to store and access files within app-specific directories. Access from internal storageFor each app, the system provides directories within internal storage where an app can organize its files. One directory is designed for your app's persistent files, and another contains your app's cached files. Your app doesn't require any system permissions to read and write to files in these directories. Other apps cannot access files stored within internal storage. This makes internal storage a good place for app data that other apps shouldn't access. Keep in mind, however, that these directories tend to be small. Before writing app-specific files to internal storage, your app should query the free space on the device. Access persistent filesYour app's ordinary, persistent files reside in a directory that you can access using the Access and store filesYou can use the To help maintain your app's performance, don't open and close the same file multiple times. The following code snippet demonstrates how to use the
Store a file using a streamAs an alternative to using the The following code snippet shows how to write some text to a file:
To allow other apps to access
files stored in this directory within internal storage, use a Access a file using a streamTo read a file as a stream, use
View list of filesYou can get an array containing the names of all files within the
Create nested directoriesYou can also create nested directories, or open an inner directory, by calling
Create cache filesIf you need to store sensitive data only temporarily, you should use the app's designated cache directory within internal storage to save the data. As is the case for all app-specific storage, the files stored in this directory are removed when the user uninstalls your app, although the files in this directory might be removed sooner. To create a cached file, call
Your app accesses a file in this directory using the
Remove cache filesEven though Android sometimes deletes cache files on its own, you shouldn't rely on the system to clean up these files for you. You should always maintain your app's cache files within internal storage. To remove a file from the cache directory within internal storage, use one of the following methods:
Access from external storageIf internal storage doesn't provide enough space to store app-specific files, consider using external storage instead. The system provides directories within external storage where an app can organize files that provide value to the user only within your app. One directory is designed for your app's persistent files, and another contains your app's cached files. On Android 4.4 (API level 19) or higher, your app doesn't need to request any storage-related permissions to access app-specific directories within external storage. The files stored in these directories are removed when your app is uninstalled. On devices that run Android 9 (API level 28) or lower, your app can access the app-specific files that belong to other apps, provided that your app has the appropriate storage permissions. To give users more control over their files and to limit file clutter, apps that target Android 10 (API level 29) and higher are given scoped access into external storage, or scoped storage, by default. When scoped storage is enabled, apps cannot access the app-specific directories that belong to other apps. Verify that storage is availableBecause external storage resides on a physical volume that the user might be able to remove, verify that the volume is accessible before trying to read app-specific data from, or write app-specific data to, external storage. You can query the volume's state by calling
For example, the following methods are useful to determine the storage availability:
On devices without removable external storage, use the following command to enable a virtual volume for testing your external storage availability logic: adb shell sm set-virtual-disk true Select a physical storage locationSometimes, a device that allocates a partition of its internal memory as external storage also provides an SD card slot. This means that the device has multiple physical volumes that could contain external storage, so you need to select which one to use for your app-specific storage. To access the different locations, call
Access persistent filesTo access app-specific files from external storage, call To help maintain your app's performance, don't open and close the same file multiple times. The following code snippet demonstrates how to call
Create cache filesTo add an app-specific file to the cache within external storage, get a reference to the
Remove cache filesTo remove a file from the external cache directory, use the
Media contentIf your app works with media files that provide value to the user only within your app, it's best to store them in app-specific directories within external storage, as demonstrated in the following code snippet:
It's important that
you use directory names provided by API constants like Query free spaceMany users don't have much storage space available on their devices, so your app should consume space thoughtfully. If you know ahead of time how much data you're storing, you can find out how much space the device can provide your app by calling
If there's enough space to save your app's data, call
The following code snippet shows an example of how your app can query free space on the device:
Create a storage management activityYour app can declare and create a custom activity that, when launched, allows the
user to manage the data that your app has stored on the user's device. You declare this custom "manage space" activity using the Ask user to remove some device filesTo request that the user choose files on the device to remove, invoke an intent that includes the
StorageStatsManager.getFreeBytes() / StorageStatsManager.getTotalBytes() Ask user to remove all cache filesAlternatively,
you can request that the user clear the cache files from all apps on the device. To do so, invoke an intent that includes the Additional resourcesFor more information about saving files to the device's storage, consult the following resources. Videos
Can a file be opened by more than one different program?To open a file in a different application:
For example, you may want to open a photo with Microsoft Paint to crop or resize it. From the desktop, right-click the desired file. From the drop-down menu, click Open with, then select the desired application. In this example, we'll select Paint.
What happens if you try to save a file with the same name into the same folder?What does the operating system do if you try to save a file with the same name into the same folder? It overwrites the original file unless you add a number, date, or initials to differentiate the files.
How can you open a file in a program that uses a different format?In the Control Panel, click the Default Programs option. Click the Associate a file type or protocol with a program option. In the Default apps window, scroll to the bottom and select Choose defaults by file type. Find the file type that interests you.
What happens when you open a program file or folder by double clicking an icon?When you double-click on the icon, the file will open in the program that it was created in. The picture to the left shows that the file was created in Notepad. However, as no arrow appears, it is not a shortcut, but indicates that this file was actually saved to the desktop.
|