简体   繁体   中英

Android: Accessing images from assets/drawable folders

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?

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

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.

我认为这两者之间的主要区别在于Drawable文件夹已编入索引并且您可以使用android 替代资源加载...所以我认为这些资源文件夹之间没有性能差异。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM