【发布时间】:2016-02-29 07:22:47
【问题描述】:
我一直在编写有关使用通用图像加载器缓存图像的代码。但它似乎并不成功,因为图像仍在从服务器加载,而不是从缓存中加载。这是我使用的代码。我希望有人能指出我的错误在哪里。谢谢。
public void setEventImage(String myImageVersion,String myImage){
ImageEvent imgEvent = (ImageView)findViewById(R.id.image_view);
String imgUrl = "url of the image"
String imgEventWidth = "";
ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.defaultDisplayImageOptions(displayImageOptions)
.imageDownloader(new BaseImageDownloader(getApplicationContext(),60*1000,60*1000))
.diskCache(new UnlimitedDiskCache(getCacheDir()))
.writeDebugLogs()
.build();
imageLoader.init(config);
imageLoader.loadImage(imgUrl, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
int width = loadedImage.getWidth();
int height = loadedImage.getHeight();
RelativeLayout.LayoutParams imageViewParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
imgEventWidth = imgEvent.getWidth();
double forivheight = (imgEventWidth * height) / width;
int finalHeight = (int) Math.round(forivheight);
imgEvent.setImageBitmap(Bitmap.createScaledBitmap(loadedImage, width, finalHeight, false));
imgEvent.setLayoutParams(imageViewParams);
}
});
}
下面是调试的日志。有一行表明图像是从网络加载的,而不是缓存。
11-27 08:44:46.772 28037-28037/com.example.me W/ImageLoader: Try to initialize ImageLoader which had already been initialized before. To re-init ImageLoader with new configuration call ImageLoader.destroy() at first.
11-27 08:44:46.782 28037-29220/com.example.me D/ImageLoader: Start display image task [http://mydomain/image/event/1.jpg?version=17_480x854]
11-27 08:44:46.782 28037-29220/com.example.me D/ImageLoader: Load image from network [http://mydomain/image/event/1.jpg?version=17_480x854]
11-27 08:44:46.852 28037-28037/com.example.me I/gralloc.sc8830: gralloc_register_buffer, handle:0x559be660, size:0x190500, fd:60
11-27 08:44:46.952 28037-28037/com.example.me I/gralloc.sc8830: gralloc_register_buffer, handle:0x557b4d38, size:0x190500, fd:71
11-27 08:44:46.982 28037-28037/com.example.me I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x55c40730, size:0x190500, fd:63
11-27 08:44:46.982 28037-28037/com.example.me I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x54c0c7f0, size:0x190500, fd:68
11-27 08:44:46.982 28037-28037/com.example.me I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x50906a58, size:0x190500, fd:48
11-27 08:44:46.982 28037-28037/com.example.me I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x55bc2058, size:0x190500, fd:54
11-27 08:44:48.023 28037-28037/com.example.me I/gralloc.sc8830: gralloc_register_buffer, handle:0x54c0ca30, size:0x190500, fd:48
11-27 08:44:48.053 28037-28037/com.example.me I/gralloc.sc8830: gralloc_register_buffer, handle:0x54c05670, size:0x190500, fd:53
11-27 08:44:48.463 28037-29220/com.example.me D/dalvikvm: GC_FOR_ALLOC freed 195K, 4% free 20235K/20876K, paused 24ms, total 24ms
11-27 08:44:48.463 28037-29220/com.example.me I/dalvikvm-heap: Grow heap (frag case) to 27.666MB for 7990288-byte allocation
11-27 08:44:48.483 28037-28046/com.example.me D/dalvikvm: GC_FOR_ALLOC freed 1K, 3% free 28036K/28680K, paused 25ms, total 25ms
11-27 08:44:49.354 28037-28037/com.example.me D/ImageLoader: Display image in ImageAware (loaded from NETWORK) [http://mydomain/image/event/1.jpg?version=17_480x854]
11-27 08:44:49.384 28037-28037/com.example.me D/dalvikvm: GC_FOR_ALLOC freed 66K, 3% free 27982K/28680K, paused 29ms, total 30ms
11-27 08:44:49.384 28037-28037/com.example.me I/dalvikvm-heap: Grow heap (frag case) to 30.300MB for 2820112-byte allocation
11-27 08:44:49.414 28037-28046/com.example.me D/dalvikvm: GC_FOR_ALLOC freed <1K, 3% free 30736K/31436K, paused 27ms, total 27ms
更新
问题是因为我使用loadedImage Bitmap设置图像:
imgEvent.setImageBitmap(Bitmap.createScaledBitmap(loadedImage, width, finalHeight, false));
我删除了上面的行并用下面的代码替换了它:
imageLoader.displayImage(imgUrl, imgEvent, displayImageOptions);
虽然这两个代码都来自通用图像加载器,但看起来我们应该这样做,以便我们可以缓存图像。
【问题讨论】:
-
“它似乎没有成功”到底是什么意思?谢谢。
-
请描述您的问题。
-
“它似乎没有成功”作为诊断的唯一输入没有帮助。
-
对不起,我的意思是图像仍然需要太多时间来加载。看起来图像仍然从服务器加载,而不是从 SD 卡加载。谢谢。
-
您需要将日志级别设置为在
Universal Image Loader上进行调试,然后您将看到加载的每个图像 URL,并且日志说明它是从哪里加载的,我看到的日志就像从 (Network或 @ 987654327@ 或Mem-Cache) 所以在这种情况下你会发现发生了什么,因为slow或take too long不应该意味着它不工作......
标签: java android universal-image-loader