Tuesday, June 23, 2009

Assets

Let's have something lighter than the application separation stuff was in the previous entry.

I got a mail from someone who wanted to embed raw files into the APK file so that they are available for the application when it is executing. I have never done such a thing but I remembered faintly something about the res/raw directory where such resources reside. They are still there but now there is a better thing: assets. The difference between a raw resource in res/raw directory and an asset is that assets behave like a file system, they can be listed, iterated over, discovered, just like files while for raw resources, you need the resource ID.

Click here to download the example program.

This simple application iterates over the files in the asset directory, reads their contents and sets three TextFields according to the content of these files. Assets go into the ./assets directory in the project root and can contain any files. The AssetManager class provides access to assets as InputStreams.

25 comments:

Anonymous said...

Hey, I know this is an old article, but I was wondering if you could give me a hand:

I am interested in storing an icon in the /assets folder, to be used as a status bar notification icon. The Notification object is constructed with a resource id for an icon that would normally sit in the /drawable folder.

For reasons specific to my application, I require the ability to store the icon file in /assets while also being able to use it as the icon for a Notification.

Is this possible?

Thanks in advance!

Gabor Paller said...

I recommend analysing the NotificationService and StatusBarService.

StatusBarService.java

NotificationManagerService.java

After a quick look, I think that NotificationManager and StatusBarManager assumes that the icon will be loaded using the icon ID which refers to the /drawable folder.

Anonymous said...

Hi! Can you access database files in that way, directly from the assets directory? Something like:

File dbfile = new File("/assets/mydb.sqlite" );
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);

It would solve the duplicate-db problem when you have your db ON the .apk AND on the /data/data//databases on the phone.

What do you think?

Thanks in advance!

Nuno

Dinesh and Android said...

Hello,
i am working with Phone Application with VideoTelephony since this two are working under one process only ,For maintainance we have to move VideoTelephony to different APK that means now two apk will be working under same process using sharedUserId can u please tell me what should be the best approach for this.?
!kindly mail me on dineshadwani2007@gmail.com

Gabor Paller said...

Dinesh, have you checked this post?

Anonymous said...

Thank you..It was of great help

ROCK YOUR LIFE said...

Thank you... !
You helped me to complete 3days effort..

A SHASHANK said...

Can u help me for whether assets will allocate memory at compile time itself i need a link or ur suggestion

Gabor Paller said...

Shashank, I don't understand your question. Assets are a set of files packaged into the APK file. If you don't load them into memory, they don't occupy any memory.

A SHASHANK said...
This comment has been removed by the author.
A SHASHANK said...

assets ->
fruits-> a.jpg ,b.jpg,c.jpg
vehicle->a.jpg,b.jpg,c.jpg
now i want to run application in such way that by Using ANT build i can able to generate apk by excluding and including the subfolders so that my apk will not take the size of both fruits and vehicle that is it should take memory based on my selection using build.xml

A SHASHANK said...

Thanks for replying me
One more doubt Gabor sir :-
that is Can we build a APK using build.xml that is when we are creating subfolders in assets . In each subfolder we are having certain images then my problem is using build.xml can i exclude or include the subfolder of assets so that at run time my size of apk should be bounded to folder that i have include hence i am trying this but i am unable to solve it that is i am getting total all subfolder size into APK , Sir can u suggest me for this problem

Gabor Paller said...

Shashank, in short: yes, you can use ant scripts with Android. Use the android update project to generate the build.xml project if you managed the project from other tool, e.g. Eclipse.

A SHASHANK said...

Sir i am doing a sample project which contains some subfolders in assets when i want to build apk then i should be able to select particular folder only so that my apk size will not expand unnecesarily .. Sir could you upload sample project into blogspot which i want . I hope you understood my problem.

Gabor Paller said...

Shashank, I definitely won't do that instead of you. :-) Set up a separate directory tree containing your asset files, delete the asset files in your Android project at the beginning of the ant script and copy the files needed for your variant into the asset directory. This will produce an APK that contains only the needed asset files. If you have a bit of experience with ant, this should not be a problem.

A SHASHANK said...

Sir the Process which you have suggested me that already i have implemented but the problem is using build.properties (which will have binary and source build) we will get the checkbox like structure for project in that structure everthing is mentioned properly. hence by checking and unchecking we can select the particular folder from assets and build an APK .But thing is that checking and unchecking is not having any affect to apk hence apk size is not shrinking hence i am asking suggestion regarding this.

Gabor Paller said...

Shashank, unzip the APK file (it is really a ZIP archive) and look, what goes into the APK file. I bet that you somehow pack in everything independently of your properties settings.

A SHASHANK said...

Sir i have done that also it is taking all the assets subfolder in it

Gabor Paller said...

Shashank, then your ant script is faulty. Either it does not delete the assets subfolder before starting to copy the relevant stuff or it copies more than needed. Fix the script.

A SHASHANK said...

Sir can u send me antscript project zip so that i can verify what is fault in my project

A SHASHANK said...

Sir are you disappointed with me , I have tried all your ideas which you gave me still i was having the problem
that's why i have you to post any antscript project so that i can verify

Vk Hooda said...

Hello....
I have a file which i will download from server.I want to put it in assets on downloading time.What will be the path i give for assets folder.
i find this but this is not working. file:///android_assets/...
It giving exception of no such like directory.
Please help me for the assets folder path.
Thanks
Yours

A SHASHANK said...

Sir please send me the Link for which i can work with antscript and load appropiate folders into my project On check and uncheck when i run it

A SHASHANK said...

Sir i am still struggling for this problem please help me out

rohit sachdeva said...

http://bluerayplus.com/Android-Assets