我在AsynTask中为创建的每个类抓取web服务器上的缩略图。对于这个特定的问题,我将在ListView中显示缩略图和一些文本属性。
问题是它很慢。我考虑在第一次需要的时候将每一张图像保存到设备上,然后每次都可以从数据库中取出图像。我不确定这是否理想,因为在服务器上图像可能更新,也可能没有更新。在我看来,我认为最好是随时抓取图片,以防有新的抓取。
我想听听你们大家对这件事的建议。
注意:我也尝试过在ListView适配器中获取映像,但是1)每次ListView被滚动时它都会获取每个映像,2)这是一个令人难以置信的错误,因为在每次图像下载时,我都可以看到图像出现在错误的列表查看项中,然后它会在一段时间后神奇地自我纠正。这并不理想,所以我把代码移到了类中。
我很感激你给我的任何建议。
下面是这个类中的图像的获取器。在第一次下载映像之后,这个功能很好,但是在最初的运行过程中却不是很好。
public Bitmap GetThumbnail_xlarge() throws Exception {
if(_thumbnail_xlarge == null ) {
if( _thumbnailBasePath != null) {
try {new DownloadImageTask(_thumbnail_xlarge).execute(_thumbnailBasePath + "/portrait_xlarge." + _thumbnailExtension); }
catch (Exception e) {} //TODO: The extension could be incorrect. Do nothing for now.
}
else{ throw new Exception("You must set the thumbnail path before getting the image"); }
}
return _thumbnail_xlarge;
}这是做这项工作的班级。请注意,这是图像属性所在的类中的一个私有类:
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
Bitmap bmImage;
public DownloadImageTask(Bitmap bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) { _thumbnail_xlarge = result; }
}在这个时候,我想不出还有什么可能是相关的。如有必要,请向我询问更多信息,并提前感谢!
发布于 2015-01-22 22:41:40
在本地保存图像以供以后使用是非常有用的。顺便说一句,这叫缓存。它很有用,因为您正在节省资源,并且可以减少使用大量资源的网络请求。
如果图像很大,可以使用WebP格式。WebP是一种“对网络上的图像进行无损和有损压缩”的图像格式,与PNG相比,WebP无损图像的大小要小26%,WebP有损图像的大小比同等SSIM索引的WebP图像小25%-34%。关于WebP格式的更多信息,您可以在这里找到:https://developers.google.com/speed/webp/?csw=1
此外,如果希望在ListView上顺利加载图像,则必须使用ViewHolder模式。更多信息在这里:http://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder
https://stackoverflow.com/questions/28099703
复制相似问题