简体   繁体   English

在Android,资产或可绘制图像中存储和获取图像的最佳方法是什么?

[英]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: 我有2种方法可以做到这一点:

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? 1-将所有图像存储在“ drawable”文件夹中,并按名称获取图像(而不是按ID获取,因为它们是根据国家/地区代码命名的),请参见此处: 如何获取具有已知资源名称的资源ID?

2- storing them in 'assets' folder. 2-将它们存储在“资产”文件夹中。

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. 并没有太大的区别,但是,我认为使用drawable文件夹可以轻松获取图像(所有图像都将在R文件中建立索引,这使得加载它们的速度更快(并且更容易!)。)从资产中使用它,那么您必须使用AssetManager然后使用AssetFileDescriptor您必须获取那些图像。

  • Assets can also be organized into a folder hierarchy, which is not supported by resources. Assets也可以组织成文件夹层次结构,资源不支持。 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]. res/drawable目录中,每个文件都有一个预编译的ID,可以通过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. 如果将资产Uri传递给ImageView之类的框架,则该框架将使用BitmapFactory从磁盘流式传输图像的降采样版本。 This is the technique it uses under the hood. 是它在后台使用的技术。

Drawable does not use this technique, for performance reasons. 由于性能原因, Drawable不使用此技术。 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. 通常不希望将大图像存储为Drawable ,并且在应用程序的生命周期中会多次加载Drawable ,因此它们是从磁盘整体流式传输并缓存在内存中的。

The above is referenced from: 上面引用自:

1) This question on SO 1)关于这个问题

2) This question on SO 2)关于这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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