Assets, Files, and Data Parsing
Android offers a few structured ways to store data, notably SharedPreferences and local SQLite databases.
And, of course, you are welcome to store your data “in the cloud” by using an Internet-based service.
Beyond that, though, Android allows you to work with plain old ordinary files, either one baked into your app (“assets”) or ones on so-called internal or external storage.
To make those files work — and to consume data off of the Internet — you will likely need to employ a parser. Android ships with several choices for XML and JSON parsing, in addition to third-party libraries you can attempt to use.
This session focuses on Assets, Raw, and Files.
1. Assets, Files, and Data Parsing
Sk. Arman Ali
Software Engineer at
W3Engineers Ltd.
2. Assets, Files, and Data Parsing
● Android offers a few structured ways to store data, notably
SharedPreferences and local SQLite databases.
● And, of course, you are welcome to store your data “in the
cloud” by using an Internet-based service.
● Beyond that, though, Android allows you to work with plain old
ordinary files, either ones baked into your app (“assets”) or
ones on so-called internal or external storage.
3. Assets, Files, and Data Parsing
● To make those files work — and to consume data off of the
Internet — you will likely need to employ a parser. Android
ships with several choices for XML and JSON parsing, in
addition to third-party libraries you can attempt to use.
● This session focuses on Assets,Raw, and Files.
4. Packaging Files with Your App
● Let’s suppose you have some static data you want to ship
with the application, such as a list of words for a spell-
checker.
● Somehow, you need to bundle that data with the
application, in a way you can get at it from Java code
later on, or possibly in a way you can pass to another
component (e.g., WebView for bundled HTML files).
● There are three main options here: raw resources, XML
resources, and assets.
5. Raw Resources
● One way to deploy a file like a spell-check catalog is to put the
file in the res/raw directory, so it gets put in the Android
application .apk file as part of the packaging process as a raw
resource.
● To access this file, you need to get yourself a Resources object.
InputStream inputStream = getResources().openRawResource(R.raw.activity_main);
6. XML Resources
● If, however, your file is in an XML format, you are better served
not putting it in res/raw/, but rather in res/xml/.
● This is a directory for XML resources – resources known to be
in XML format.
● To access that XML, you once again get a Resources object by
calling getResources() on your Activity or other Context.
XmlResourceParser xmlResourceParser = getResources().getXml(R.xml.test);
7. Assets
● Your third option is to package the data in the form of an asset.
● You can create an assets/ directory in your source set (e.g.,
src/main/assets), then place whatever files you want in there.
● Those are accessible at runtime by calling getAssets() on your
Activity or other Context, then calling open() with the path to
the file (e.g., assets/foo/index.html would be retrieved via
open("foo/index.html")).
InputStream inputStream = getAssets().open("foo/index.html");
8. Files on device storage
● Android uses a file system that's similar to disk-based file
systems on other platforms.
● All Android devices have two file storage areas: "internal" and
"external" storage.
9. Files on device storage
Internal storage:
● It's always available.
● Files saved here are
accessible by only your
app.
● When the user uninstalls
your app, the system
removes all your app's files
from internal storage.
External storage:
● It's not always available, because the user
can mount the external storage as USB
storage and in some cases remove it from
the device.
● It's world-readable, so files saved here
may be read outside of your control.
● When the user uninstalls your app, the
system removes your app's files from here
only if you save them in the directory from
getExternalFilesDir().
10. Internal Storage
● Android can save files directly to the device internal storage.
● These files are private to the application and will be removed
if you uninstall the application.
● We can create a file using openFileOutput() with parameter as
file name and the operating mode.
11. Internal Storage Contd…
● Similarly, we can open the file using openFileInput() passing
the parameter as the filename with extension.
● File are use to store large amount of data Use I/O interfaces
provided by java.io.* libraries to read/write files.
12. File Operation(Read)
● Use context.openFileInput(string name) to open a private
input file stream related to a program.
● Throw FileNotFoundException when file does not exist.
● Syntax:-
fileinputStram.in=this.openfileinput(“xyz.txt”)
. . . . .
In.close()://Close input stream
13. File Operation (Write)
● Use context.openFileOutput(string name,int mode ) to open a
private output file stream related to a program.
● The file will be created if it does not exist.
● Passing MODE_PRIVATE makes it private to your app.
● Output stream can be opened in append mode, which means
new data will be appended to end of the file.
15. Write a cache file
● For cache some files, you should use createTempFile().
● Syntax:-
private File getTempFile(Context context, String url) {
File file;
try {
String fileName = Uri.parse(url).getLastPathSegment();
file = File.createTempFile(fileName, null, context.getCacheDir());
} catch (IOException e) {
// Error while creating file
}
return file;
}
16. Open a directory
● getFilesDir()
○ Returns a File representing the directory on the file system that's uniquely associated with your app.
● getDir(name, mode)
○ Creates a new directory (or opens an existing directory) within your app's unique file system directory.
● getCacheDir()
○ Returns a File representing the cache directory on the file system that's uniquely associated with your
app.
○ This directory is meant for temporary files, and it should be cleaned up regularly.
○ The system may delete files there if it runs low on disk space, so make sure you check for the
existence of your cache files before reading them.
17. External Storage
● Every Android-compatible device supports a shared “external
storage” that you can use to save files
○ Removable storage media (such as an SD card)
○ Internal (non-removable) storage
● File 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 computer.
● These files are private to the application and will be removed
when the application is uninstalled.
18. External Storage Contd...
● Must check whether external storage is available first by calling
getExternalStorageState()
○ Also check whether it allows read/write before reading/writing on it
● getExternalFilesDir() takes a parameter such as
DIRECTORY_MUSIC,DIRECTORY_RINGTONE etc,to open specific
type of subdirectories.
● For public shared directories,use
getExternalStoragePublicDirectory()
19. Query free space
● getFreeSpace()
○ provide the current available space
● getTotalSpace()
○ Provide the total space in the storage volume
20. Delete a file
● You should always delete files that your app Assets, Files,
and Data Parsing.
● The most straightforward way to delete a file is to call delete()
on the File object.