简体   繁体   中英

What is the best way to store and get images in Android, assets or drawable?

I have to make a list of countries with the name of the country and its flag appears in each item.

I have 2 ways to do this:

1- storing all images in 'drawable' folder and getting them by name (not by id, since they're named based on country code) see here: How to get a resource id with a known resource name?

2- storing them in 'assets' folder.

In terms of performance, which way is better?

There is not much difference, however, I think using drawable folder you can get images easily (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

If you are passing an asset Uri to something like an ImageView , the framework will use BitmapFactory to stream a downsampled version of the image from disk. This is the technique it uses under the hood.

Drawable does not use this technique, for performance reasons. It is not generally expected that huge images are stored as Drawable , and Drawable loading happens many times over your app's lifecycle, so they are streamed from disk in their entirety and cached in memory.

The above is referenced from:

1) This question on SO

2) This question on SO

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