简体   繁体   English

android通用图像加载器无法缓存或保存图像

[英]android universal image loader not caching or saving image

I am using universal image loader to load images. 我正在使用通用图像加载器加载图像。 The issue is that It does not save loaded images in cache Here is my code. 问题是它不会将加载的图像保存在缓存中这是我的代码。

    Map<String, String> headers     = new HashMap<String, String>();    
    headers.put("key", Commons.CURRENT_ACTIVE_PROFILE.getKey()); 
    headers.put("secret", Commons.CURRENT_ACTIVE_PROFILE.getSecret());  

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(mActivity)
    .imageDownloader(new CustomImageDownaloder(mActivity)).build(); 

    //imageLoader.init(ImageLoaderConfiguration.createDefault(mActivity)); 

    imageLoader.init(config);  


    imageLoader = ImageLoader.getInstance();
    displayImageOptions = new DisplayImageOptions.Builder()
    .extraForDownloader(headers)
    .showImageForEmptyUri(R.drawable.no_preview)
    .showImageOnLoading(R.drawable.no_preview) 
    .showImageOnFail(R.drawable.no_preview)
    .cacheInMemory(true) 
    .considerExifParams(true)
    .bitmapConfig(Bitmap.Config.RGB_565)
    .build();

The problem is when I use following line for ImageLoaderCinfiguration that creates a default settings It would work fine and saves the images in cache. 问题是当我在ImageLoaderCinfiguration的以下行中创建默认设置时,它将正常工作并将图像保存在缓存中。

imageLoader.init(ImageLoaderConfiguration.createDefault(mActivity));

But I want to use custom settings for ImageLoaderConfiguration because I am using CustomImageDownloader to pass key/secret with URL. 但是我想对ImageLoaderConfiguration使用自定义设置,因为我正在使用CustomImageDownloader来传递带有URL的密钥/秘密。 So i use following lines for config 所以我使用以下行进行配置

 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(mActivity)
.imageDownloader(new CustomImageDownaloder(mActivity)).build();

But In this case it doest not save image and if I run the app without internet it does not load the images. 但是在这种情况下,它不会保存图像,并且如果我在没有互联网的情况下运行应用程序,则不会加载图像。 Any help/suggestion please? 有什么帮助/建议吗?

Possible duplicate I have tried that but no use. 可能重复的我尝试过,但没有用。

U can use this simple method for Image Loader view U可以使用此简单方法进行Image Loader视图

private void initImageLoader() {
    // ImageLoader initializing
    DisplayImageOptions opts = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true).build();
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
            .defaultDisplayImageOptions(opts).build();
    ImageLoader.getInstance().init(config);
}

I have resolved the issue myself. 我已经解决了这个问题。 as using 作为使用

.imageDownloader(new CustomImageDownaloder(mActivity)).build(); 

would override the default imageDownloader that is 会覆盖默认的imageDownloader

.imageDownloader(new BaseImageDownloader(getApplicationContext())) // default

Default image downloader class contains diffrent methods to load images from web, local storage, assets etc. But the CustomImageDownaloder that i have created only contains code to get stream from web/network. 默认图像下载器类包含从Web,本地存储,资产等加载图像的不同方法。但是我创建的CustomImageDownaloder仅包含从Web /网络获取流的代码。 So what I did is to copy and paste all the code of default BaseImageDownloader to CustomImageDownaloder and just modify the 所以我要做的是将默认BaseImageDownloader的所有代码复制并粘贴到CustomImageDownaloder,然后修改

getStreamFromNetwork(String imageUri, Object extra)

according to my requirements. 根据我的要求。

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

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