Android Beginners: Intro to Resources and Assets

by Ivan.Memruk  on  14. June 2010 11:34

Managing application resources and assets, such as bitmap files, sounds and other static data that is used by your application, is something that is done differently on each specific mobile OS. The Android platform has a well-thought approach to application resource management, but it might seem somewhat confusing to beginners. This article describes the difference between Android resources and assets, how you can access them, and gives suggestions on their proper usage in your Android apps.

Every application needs resources, regardless of the platform it runs on. Mobile apps are no exception. Here are some examples of resources your Android application might need:

  • An application icon that is displayed in the app lis t
  • A logo bitmap that you want display on your first activity
  • An MP3 file that contains a sound effect for your game
  • A WSDL file for a web service that you contact from your app

Android gives you two major ways to handle resources – Resources and Assets - and we will discuss both in this article.

Part 1: Resources

Let’s start with the first approach to Android app resource management – called just Resources.

After you create your Android app in Eclipse using ADT (here’s an article that might help you get started with that), one of the directories created by the Android Project wizard is called res:

This is a magic directory that is treated in a special way during the Android app build process (which happens every time you modify your application in Eclipse). As the result, resources you put into that directory will have the following special properties:

  • Each resource is assigned a unique precompiled ID that is defined inside the auto-generated R class. The R class is generated in your application package and you should never try to modify it, but you can and will use it in other classes that you write yourself.
    • For example, the icon.png file that is located in the res/drawable folder will have a constant unique int value assigned to it in the R class. You can address it in your code as R.drawable.icon, and many image-related operations, such as setting the image for an ImageView, will have a method that accepts a resource ID as an int parameter. See for example: ImageView.setImageResource().
  • Thanks to the extensive support for resource IDs in various Android APIs, you will very rarely need to load your resources manually, using streams and parsers. Just look for an operation that supports a resource ID and will load it internally. Examples:
    • TextView.setText() – allows you to set a text view value to a text resource, for example defined in the default res/values/strings.xml.
    • BitmapFactory.decodeResource() – loads a bitmap from a resource. This operation wants you to provide a Resources instance too, so here you can use the getResource() method that is included in your activity class.
  • You can refer to resources when defining other resources. For example, here’s a layout definition that refers to a string from res/values/strings.xml (the @ sign is used to indicate a resource reference):









    android:text="@string/hello" />



However, if you want to store your resources as Resources (in the res folder) and enjoy all the benefits, you have to remember the following rules:

  • Resources are always stored in a fixed hierarchy. For example, you can’t organize your bitmaps into folders. They will always be R.drawable.*
  • Resource file names must be valid Java identifiers, for example they must start with a letter and may not contain any special symbols. (Eclipse ADT will remind you if you forget.)
  • Resource IDs are not typed, so a piece of code where you pass a text resource as an ImageView image will compile and then break at the run-time. Be careful.
  • You have to store resources in the correct, predefined subdirectories of res, according to their type. For example, all images go to res/drawable and layouts to res/layout. If in doubt, consult the Android Dev Guide.

This was just an intro to the powerful Resource subsystem of Android. I hope you have acquired the basic knowledge you need to proceed with your first apps. You can learn a lot more in the Android Dev Guide, in the Resources section.

Part 2: Assets

If you did any Java development before Android, then Assets should be very familiar to you. Basically, Assets are a raw approach to resource management. You can place any files you want into the assets directory of your project:

Then, if you have a reference to any Context subclass, such as an Activity, you can get a reference to the AssetManager instance provided by the platform:

    AssetManager assetManager = getAssets();

Once you have an AssetManager reference, you can just open a raw InputStream for any asset you put into the assets directory like this:

        InputStream stream = null;

        try {

            stream ="generated-keys.bin");

        } catch (IOException e) {

            // handle



Of course, you have to close the stream in finally, as in any Java application:

        finally {

                  if (stream != null) {

                        try {


                        } catch (IOException e) {}




Unlike Resources, Assets can have any filename that is supported by OS and can be organized into subfolders in the assets directory. However, the only thing you can do with an asset is get an input stream. Thus, it does not make much sense to store your strings or bitmaps in assets, but you can store custom-format data such as input correction dictionaries or game maps. In fact, Android platform designers took care to support most of your app resource use cases within the Resources framework, so you won’t need to use Assets at all in most typical apps.


Android API includes a very comfortable Resources framework that is also optimized for most typical use cases for various mobile apps. You should master Resources and try to use them wherever possible.

However, if you need more flexibility for your special case, Assets are there to give you a lower level API that allows organizing and processing your resources with a higher degree of freedom.

Ivan Memruk

Related articles

Should Android market be more regulated?
Android is undeniably one of the hottest products available to customers living in this era of tec...
15 Android features iPhone users will envy
The Android platform dubbed as the “The one of the most liberal platforms in the world&rdquo...
Intro to the three Android Audio APIs
Mobile devices of the first generation were not capable of playing any sounds except ...

blog comments powered by Disqus
� 2010 WiseAndroid