14.01.2013 Views

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6: Data Storage, Retrieval, and Sharing<br />

To access these Read Only fi le resources, call the openRawResource method from your application’s<br />

Resource object to receive an InputStream based on the specifi ed resource. Pass in the fi lename<br />

(without extension) as the variable name from the R.raw class, as shown in the skeleton code below:<br />

Resources myResources = getResources();<br />

InputStream myFile = myResources.openRawResource(R.raw.myfilename);<br />

Adding raw fi les to your resources hierarchy is an excellent alternative for large, preexisting data<br />

sources (such as dictionaries) where it’s not desirable (or even possible) to convert them into an<br />

Android database.<br />

Android’s resource mechanism lets you specify alternative resource fi les for different languages,<br />

locations, or hardware confi gurations. As a result, you could, for example, create an application that<br />

dynamically loads a dictionary resource based on the user’s current settings.<br />

File Management Tools<br />

Android supplies some basic fi le management tools to help you deal with the fi lesystem. Many of these<br />

utilities are located within the standard java.io.File package.<br />

Complete coverage of Java fi le management utilities is beyond the scope of this book, but Android does<br />

supply some specialized utilities for fi le management available from the application’s Context.<br />

❑ deleteFile Lets you remove fi les created by the current application.<br />

❑ fileList Returns a String array that includes all the fi les created by the current application.<br />

Databases in Android<br />

Android provides full relational database capabilities through the SQLite library, without imposing any<br />

additional limitations.<br />

Using SQLite, you can create independent, relational databases for each application. Use them to store<br />

and manage complex, structured application data.<br />

All Android databases are stored in the /data/data//databases folder on your<br />

device (or emulator). By default, all databases are private, accessible only by the application that created<br />

them. To share a database across applications, use Content Providers, as shown later in this chapter.<br />

Database design is a vast topic that deserves more thorough coverage than is possible within this book.<br />

However, it’s worth highlighting that standard database best practices still apply. In particular, when<br />

creating databases for resource-constrained devices, it’s important to reduce data redundancy using<br />

normalization.<br />

The following sections focus on the practicalities of creating and managing SQLite databases in Android.<br />

175

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!