Use Raw Resources

Description

Android allows raw files in addition to arbitrary XML files. These resources, placed in /res/raw folder.

The raw files could be audio, video, or text files that require localization or references through resource IDs.

These files are not compiled but are moved to the application package as they are.

Each file has an identifier generated in R.java.

Example

If you were to place a text file at /res/raw/test.txt, you would be able to read that file using the following code.


String getStringFromRawFile(Activity activity)
throws IOException{
   Resources r = activity.getResources();
   InputStream is = r.openRawResource(R.raw.test);
   String myText = workOnInputStream(is);
   is.close();/*ww w .j  a  v a  2 s  . c om*/
   return myText;
}

File names with duplicate base names generate a build error.





















Home »
  Android »
    Android Basics »




Hello Android
Resources
Activity
Calendar
Camera
Contact
Content Provider
Database
Hardware
Intent
Location
Media
Network
Notification
Preference
Sensor
Service
SMS