private void setImageloadConfig() {
defaultOption = new DisplayImageOptions.Builder()
.bitmapConfig(Config.RGB_565).resetViewBeforeLoading(false)
.cacheInMemory(true).cacheOnDisk(true);
configuration = new ImageLoaderConfiguration.Builder(context).denyCacheImageMultipleSizesInMemory()
.threadPoolSize(3).memoryCache(new LruMemoryCache(IMG_MAX_SIZE))
.memoryCache(new UsingFreqLimitedMemoryCache(IMG_MAX_SIZE)).memoryCacheSize(IMG_MAX_SIZE)
.memoryCacheSizePercentage(13).diskCache(new UnlimitedDiscCache(new File(IMAGE_FILE_PATH)))
.diskCacheSize(50 * 1024 * 1024).diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
.defaultDisplayImageOptions(defaultOption.build())
.imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000)).build();
ImageLoader.getInstance().init(configuration);
}
In global activity, I have set DisplayImageOptions, but there is a problem, if I want to config showImageOnLoading, showImageForEmptyUri and showImageOnFail, how can solve it, because in difference issue. I want to set different image, but if I set it in specific activity, it will cover the global configuration.
The problem exists in code part2.
DisplayImageOptions options = new DisplayImageOptions.Builder()
.bitmapConfig(Config.RGB_565).resetViewBeforeLoading(false)
.cacheInMemory(true).cacheOnDisk(true)
.showImageOnLoading(R.drawable.home_list_img_picture)
.showImageForEmptyUri(R.drawable.home_list_img_picture)
.showImageOnFail(R.drawable.home_list_img_picture)
.build();
ImageLoader.getInstance().displayImage(imgPath,iamge_photo,options);
If I move the DisplayImageOptions configuration from global activity to specific activity, it's OK, but causes another problem. When I switch activities, the image component will flicker, which is OK, when configured in global activity, how can I solve this confict, any suggestions?
Related
Using an Universal Image loader i'm trying to increase the size of the image which i got from JSON url and i tried all possible methods available in valuable stackoverflow but didn't get the right solution to increase the image size so here i am posting my code and correct me by giving your suggestions.Thanks in advance
public ImageSlideAdapter(FragmentActivity activity, List<Product> products,
HomeFragment homeFragment) {
this.activity = activity;
this.homeFragment = homeFragment;
this.products = products;
options = new DisplayImageOptions.Builder()
.showImageOnFail(R.drawable.ic_error) .bitmapConfig(Bitmap.Config.ARGB_8888)
.imageScaleType(ImageScaleType.EXACTLY)
.showImageForEmptyUri(R.drawable.ic_empty).cacheInMemory() .displayer(new RoundedBitmapDisplayer(20))
.cacheOnDisc().build();
imageListener = new ImageDisplayListener();
}
I have installed a xamarin Picasso in my application, but wanted to clear the cached when the user logout my application. I can't find a ClearCache method which original Picasso library has one.
Not sure if you mean the memory or disk cache, so:
Disk cache:
Picasso defines its disk cache name as (PICASSO_CACHE = "picasso-cache"), so if you are not using a custom disk cache, you can delete the application's Picasso defined disk cache directly:
_picasso.Dispose(); // Done using Picasso
var cache = new File(BaseContext.ApplicationContext.CacheDir, "picasso-cache");
if (cache.Exists())
{
cache.Delete();
}
// Recreate if needed, but Picasso Build() will recreate it if it does not exist
if (!cache.Exists())
{
cache.Mkdirs();
}
If you are using your own LruCache or custom disk Cache implementation:
var cache = new File(BaseContext.ApplicationContext.CacheDir, "picasso-cache");
if (!cache.Exists())
cache.Mkdirs();
_lruCache = new LruCache((int)Runtime.GetRuntime().MaxMemory() / 1024 * 8);
_diskLruCache = new DiskLruCache(cache, 10 * 1024 * 1024);
_picasso = new Picasso.Builder(BaseContext).MemoryCache(_lruCache).Downloader(new OkHttpDownloader(_OkHttp3Client)).IndicatorsEnabled(true).Build();
Clear memory:
_lruCache.Clear();
Clear disk cache via your custom disk cache implementation:
_diskLruCache.Delete(); // Assumes Android style DiskLruCache
I am using Universal imageloader class with RoundedBitmapDisplayer options. The RoundedBitmapDisplayer takes a particular value (here 1000) whose documentation is not available. Does anyone know what the value represents ?
private DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true).cacheOnDisk(true).resetViewBeforeLoading(true)
.displayer(new RoundedBitmapDisplayer(1000)).showImageForEmptyUri(R.drawable.ico_user)
.showImageOnFail(R.drawable.ico_user).showImageOnLoading(R.drawable.ico_user).build();
I need to load Bitmaps into ArrayList, then convert it to Bitmap[] and pass into ArrayAdapter to inflate ListView. I use UniversalImageLoader library and here is my code:
final ArrayList<Bitmap> imgArray = new ArrayList<>(); //before the method scope, as a class field
//...some code...
File cacheDir = StorageUtils.getOwnCacheDirectory(
getApplicationContext(),
"/sdcard/Android/data/random_folder_for_cache");
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true).cacheOnDisc(true).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
getApplicationContext()).defaultDisplayImageOptions(options)
//.discCache(new FileCounterLimitedCache(cacheDir, 100)) - I commented it 'cause FileCounterLimitedCache isn't recognized for some reason
.build();
ImageLoader.getInstance().init(config);
for (int num=0;num<4;num++) {
ImageLoader imageLoader = ImageLoader.getInstance();
final int constNum = num;
imageLoader.loadImage("http://example.com/sample.jpg", new SimpleImageLoadingListener()
{
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
{
imgArray.add(constNum, loadedImage);
}
});
}
But I have some issues to struggle. Firstly, it often returns error (IndexOutOfBoundsException - current index exceeds size of ArrsyList) when first run. Then after some time (about a minute) the size of ArrayList is 1 (I check it with Toast), and then when run again right at once, it's already 4 (as it needs to be). Strange. But the main thing I need that the ArrayList is first filled and then all the other actions are done (that they be delayed and I don't have errors on the first run). How to do it?
And what to do if somebody doesn't have SD card? Btw, I couldn't find the created cache folder on my SD...
You can't add the elements in the way you're trying to. If for example the 2nd image finishes loading first, your code will correctly throw an IndexOutOfBoundsException as the location you're trying to add at is beyond the current size - see the documentation
You might be better off using an array initialised to the number of elements seeing as you know it's 4 elements - e.g.
final Bitmap[] imgArray = new Bitmap[4];
Then add the elements in your onLoadingComplete() using
imgArray[constNum] = loadedImage;
You can use a SparseArray instead of an ArrayList to avoid the IndexOutOfBoundsException.
I assume this is what you are after:
final SparseArray<Bitmap> imgArray = new SparseArray<>(); //before the method scope, as a class field
int numberOfImages;
int numberOfLoadedImages;
//...some code...
File cacheDir = StorageUtils.getOwnCacheDirectory(
getApplicationContext(),
"/sdcard/Android/data/random_folder_for_cache");
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true).cacheOnDisc(true).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
getApplicationContext()).defaultDisplayImageOptions(options)
//.discCache(new FileCounterLimitedCache(cacheDir, 100)) - I commented it 'cause FileCounterLimitedCache isn't recognized for some reason
.build();
ImageLoader.getInstance().init(config);
numberOfImages = 4;
numberOfLoadedImages = 0;
for (int num=0;num<4;num++) {
ImageLoader imageLoader = ImageLoader.getInstance();
final int constNum = num;
imageLoader.loadImage("http://example.com/sample.jpg", new SimpleImageLoadingListener()
{
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
{
imgArray.put(constNum, loadedImage);
numberOfLoadedImages++;
if(numberOfImages == numberOfLoadedImages)
{
//Do all the other actions
}
}
});
}
Universal Image Loader library contains the following method:
public void displayImage(java.lang.String uri, android.widget.ImageView imageView)
Which can be passes the image url and the ImageView to display the image on it.
plus to that, if later the same ImageView passed to the method but with a different url two thing can happen:
if old image already downloaded, normally the new image will be set to the ImageView.
if old image still downloading the library will cancel the http connection that is downloading the old image. and start downloading
the new image. (can be observed in the LogCat). this behaviour happens
when using an adapter.
Method call:
ImageLoader.getInstance().displayImage("http://hydra-media.cursecdn.com/dota2.gamepedia.com/b/bd/Earthshaker.png", imageView1);
Configuration:
Caching will not work if the defaultDisplayImageOptions not specified. which tells the library where to save those image. because this library has options to load images from Assets, Drawables or Internet:
DisplayImageOptions opts = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).build();
With this option the images will be saved in app. internal memory.
don't worry weather the device have an external memory or not.
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
.defaultDisplayImageOptions(opts)
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
.memoryCacheSize(2 * 1024 * 1024)
.diskCacheSize(50 * 1024 * 1024)
.diskCacheFileCount(100)
.writeDebugLogs()
.build();
ImageLoader.getInstance().init(config);
I created a working github repository for it. check it out.
I use Glide library for Android. I want to set the cache in my custom folder, so the standard cache folder can be clean (with Master Clean for example).
For this reason I use this code from manual, but this don't work for me.
My code:
DiskCache.Factory diskCacheFactory = new DiskCache.Factory() {
#Override
public DiskCache build() {
DiskCache diskCache = DiskLruCacheWrapper.get(getFilesDir(), 1024*1024*100);
return diskCache;
}
};
new GlideBuilder(this).setDiskCache(diskCacheFactory);
Glide.with(this)
.load("http://www.website.com/1.jpg")
.into(imageView);
After I run this app Glide saves the image in the default folder.
In Glide 3.5, Glide.isSetup() and Glide.setup() are deprecated. The best way to do this is to use GlideModules to do this kind of configuration lazily. Check out the wiki page on configuration.
Try use:
if (!Glide.isSetup()) {
GlideBuilder gb = new GlideBuilder(this);
DiskCache dlw = DiskLruCacheWrapper.get(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/myCatch/"), 250 * 1024 * 1024);
gb.setDiskCache(dlw);
Glide.setup(gb);
}