28

The app I am currently working on has hundreds of images. At the moment I store them in the 'Drawable' folder. I was thinking about moving all of them to Assets folder.

My question is: Is there any difference in performance when using both approaches?

Marqs
  • 17,800
  • 4
  • 30
  • 40
  • Really good questions, I would suspect there is a slight difference as the images lying in the drawable folder is indexed... But I don't have any facts on me... – Warpzit Dec 06 '11 at 12:53
  • You can refer this link:- http://stackoverflow.com/questions/5583608/android-difference-between-res-and-assets-directory – anddev Dec 06 '11 at 13:03
  • @Warpzit - your slight difference is the indexing of /res directory in R file.. – user370305 Dec 06 '11 at 13:10
  • 1
    I only see people through around with baseless comments, plz do a test with a counter to figure out whats fastest :) I'm pretty sure drawable is faster because of the indexing, but I'm not sure how much (probably nearly no difference) – Warpzit Dec 06 '11 at 13:44

4 Answers4

10

I don't think so there is bit difference in performance of using these two folders, I think using drawable folder you can get easily images (All will be indexed in the R file, which makes it much faster (and much easier!) to load them.), and If you want to use it from asset then you have to use AssetManager then using AssetFileDescriptor you have to get those images.

  • Assets can also be organized into a folder hierarchy, which is not supported by resources. It's a different way of managing data. Although resources cover most of the cases, assets have their occasional use.

  • In the res/drawable directory each file is given a pre-compiled ID which can be accessed easily through R.id.[res id]. This is useful to quickly and easily access images, sounds, icons...

user370305
  • 108,599
  • 23
  • 164
  • 151
  • 2
    Yes it really is annoying not to have folder hierarchy in drawble folders. That forces me to name my icons with stupidly long descriptive names like main_menu_page_3_return... – Guillaume Dec 06 '11 at 13:12
  • @Guillaume - also there is no worried for spelling mistake if we use res directory. :-) – user370305 Dec 06 '11 at 13:19
  • 2
    @userXYZ - I don't know if that's such a good thing, I would prefer my spelling mistakes be pointed to me with an error rather than allowed to carry to production deployment... This reminds me of a mistake I did when I was a junior, using some automated SQL generator from my classes: I had a column called penId, but I mistakenly put an "s" instead of the "d" (they are close on the keyboard...). You can imagine my embarassment and my client surprise when he looked at the database... – Guillaume Dec 06 '11 at 13:25
  • Im forced to use assets now as i cannot access the drawable folder of the downloaded module my 'instant app' calls for the instalation version, as I havent been able to reach its R file. – Androidcoder Oct 16 '19 at 20:28
3

As far as I know, there shouldn't be big difference but I would rather go with Drawable.

Drawable gives you, beside indexing, option to fight with screen density fragmentation and Assets shouldn't depend on density. Having big pictures on smaller screens and/or lower specification phones (RAM, CPU) can cause real trouble and influence usability and app responsivity.

vbokan
  • 133
  • 6
2

I think that the main difference between these two is that Drawable folder is indexed and you can gain use of the android Alternative resources load... So I think that there is no difference of performance between these resource folders.

Cata
  • 11,133
  • 11
  • 65
  • 86
-1

Actually, i also met the problems. i want my App's ImageView's image is loaded from assets/ instead of res/drawables, so that i am able to update my APP from internet dynamically.

From my test/feeling the performance is likely to be very different if you do not pay attention.

         //v.setImageBitmap(ImageUtil.getBitmapFromAsset(this, "data/dog/dog_0001.png", 60, 60));
        //v.setImageDrawable(ImageUtil.getDrawableFromAsset(this, "data/dog/dog_0001.png"));
        v.setImageResource(R.drawable.dog_0001);

my app will do above in Activity's onCreate. it seems proved that setImageResource is fastest.

the reason from my point is that the decode from asset will take time which will block UI thread, while set the resource ID is faster than the decoding. i saw the code in setImageResource which seems also load the image. i am not sure why my decode is slower than setImageResource. but i do feel setImageResource is fastest. i am trying to let the decode asyncly decode and then set bitmap.

RyanShao
  • 429
  • 2
  • 9