Android的 - 高效的方式来加载多个图像的远程服务器多个、高效、图像、加载

2023-09-05 09:51:14 作者:我在地狱你可来

我有一个Android应用程序,将检索从PHP远程服务器上的数据(图像+文字),并在GridView显示出来。 我使用的装载机所做的操作在后台。我对图像和单独的连接,因为文本检索图像将需要更长的时间,我想立即显示文本。该文本是连接使用JSON从MySQL检索后在服务器上的codeD。在应用程序,我解析的JSON对象和显示文本,因为我需要。

I have an Android application that would retrieve data (images+text) from a php remote server and display them in a GridView. I am doing the operation in the background using Loaders. I have separate connections for images and texts since retrieving images would take longer and I want to display the texts immediately. The texts are encoded with Json on the server after being retrieved from MySQL. On the app, I am parsing the Json Objects and displaying the texts as I need.

问题是与图像。我不知道,如果使用JSON编码的图像将是一个不错的主意。另外,图像保存为BLOB在数据库中,以EN code他们使用JSON,我需要使用base64_en code()之前,它是没有效率。我看到这个帖子很多,但它总是一个简单的例子,当你必须得到一个图像。在我来说,我会检索多达30个小尺寸的图像。

The problem is with images. I am not sure if encoding the images with Json would be a good idea. Also the images are saved as blob in the database, in order to encode them with Json I need to use base64_encode() before which is not efficient. I have seen many posts about this, but it’s always a simple example when you have to get one image. In my case I’ll be retrieving up to 30 small-size images.

我的问题是,我可以继续我刚才presented,但似乎应该有更好的方式来做到这一点。你觉得这个怎样?我是不是走错路了?

My question is, I can proceed with what I just presented, but it seems that there should be a better way to do this. What do you think about this? Am I going the wrong way?

另外,我在想,如果我能在GridView单独显示每个图像,一旦它已准备就绪(而不是等待所有的图像做好准备),就像在谷歌Play应用的GridView控件。我可以采取什么方法来实现这一目标?

Also I was thinking if I can display each image separately in the gridview once it is ready (not waiting for all the images to be ready) just like in the "Google Play App"’s GridView. What approach can I take to achieve this?

在此先感谢乡亲!

推荐答案

在我眼中最好的办法是将下载的图像文件作为通过HTTP正常的图像文件get请求。请确保它是线程,当然,并可以请求排队到线程池,并有2-3个线程经历和下载。

Best approach in my eyes would be to download the image files as normal image files via a HTTP get request. Make sure it is threaded of course, and have a thread pool that you can queue up requests into, and have 2-3 threads go through and download.

在他们节省方面,我个人储蓄向BLOB在数据库中搬走,并选择将它们保存到持久化存储在应用程序的私有目录。保存他们的文件名作为其在你所创建的数据库ID的图像文件将加载它们放回快得多。

In terms of saving them, I would personally move away from saving to blob in a database, and opt to save them to the persisted storage in your application's private directory. Saving the image files with their filename as their id in the database you have created will be much quicker for loading them back in.

您也可以持有引用的ImageView,并将它显示一个占位符开始,以一个成功的HTTP请求更换你刚才下载一个ImageView的位图/从存储器读

You can also hold a reference to the ImageView, and have it display a place-holder initially, with a successful HTTP request replacing the bitmap of the ImageView with the one you have just downloaded/read in from storage.

您也可以做你做的HTTP请求中的一些图像缓存。

You can also do some image caching within the HTTP request you make.

ImageView myImageView = findViewById(R.id.testImage);
URL url = new URL("http://www.website.com/image.jpg");
URLConnection connection = url.openConnection();
connection.setUseCaches(true);
Object response = connection.getContent();
if (response instanceof Bitmap) {
  Bitmap bitmap = (Bitmap)response;
  myImageView.setBitmap(bitmap);
} 

有也可能是有帮助来查找LRUCache,这对于你进行了大量的缓存功能的用途。

It also may be helpful to lookup the uses of the LRUCache, which performs a lot of caching functionality for you.

查看这个环节在Android开发者网站为深入指导了良好的图像缓存

Check out this link at the Android Developer site for a good in depth guide to image caching

编辑: 您可以使用建议的罗伯特·朗特里的回答更有效地加载位图,以减少你的内存使用为好。使用较少的内存,而这将工作做好,如果你是从网上下载较大的图像创建缩略图并保存关闭到本地存储的链接提供的详细信息加载位图。

You can use the advice in Robert Rowntree's answer to load bitmaps more efficiently to cut down on your memory use as well. The link provided details loading of bitmaps using less memory, something that would work well if you are creating thumbnails from larger images downloaded over the web and saved off to local storage.