Android: Accessing images from assets/drawable fol

2019-01-14 11:06发布

问题:

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?

回答1:

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...



回答2:

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.



回答3:

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.



回答4:

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.