I'm using Universal Image Loader in a ListView, It works perfectly the first time, but the rest of the time, the image has no rounded corners. Only if I scroll the image has rounded borders again.
This is my code:
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
context).threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO).enableLogging()
.build();
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config);
DisplayImageOptions options = new DisplayImageOptions.Builder()
.displayer(new RoundedBitmapDisplayer(50))
.showStubImage(R.drawable.ic_app)
.showImageForEmptyUri(R.drawable.camera)
.showImageOnFail(R.drawable.ic_error).cacheInMemory().cacheOnDisc()
.build();
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.listmessages_row, null);
}//End if
ImageView avatar = (ImageView) convertView.findViewById(R.id.ImageView_MessageRow);
ImageView avatarEmpty = (ImageView) convertView.findViewById(R.id.ImageView_PhotoProfileEmpty);
final int positionAux = position;
if (listItems.get(position).avatar.equals("no_avatar")){
avatarEmpty.setVisibility(View.VISIBLE);
avatar.setVisibility(View.GONE);
}else{
avatarEmpty.setVisibility(View.GONE);
avatar.setVisibility(View.VISIBLE);
imageLoader.displayImage(IMAGES + listItems.get(position).avatar, avatar, options);
}//End if-else
return convertView;
}//End getView
I answer myserlf, I modified the options of displayImageOtions and it works perfectly all the times. I only deleted this line: cacheInMemory()
Now my display image options are:
options = new DisplayImageOptions.Builder()
.displayer(new RoundedBitmapDisplayer(50))
.showStubImage(R.drawable.ic_app)
.showImageForEmptyUri(R.drawable.camera)
.showImageOnFail(R.drawable.ic_error)
.cacheOnDisc()
.build();
Related
I'm using universal image loader, but the images are reloaded every time I scroll up and down.
I'm using below code.
Configuration - >
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.memoryCache(new WeakMemoryCache())
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
// .enableLogging() // Not necessary in common
.build();
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config);
Options->
options = new DisplayImageOptions.Builder()
.resetViewBeforeLoading()
.imageScaleType(ImageScaleType.EXACTLY)
.cacheInMemory(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
view ->
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View workingview = inflater.inflate(R.layout.gridview_item_photo_icon, null);
ImageView imageView = (ImageView) workingview.findViewById(R.id.picture);
ImageAware imageAware = new ImageViewAware(imageView, false);
imageLoader.displayImage("file://" + imageUrls.get(position), imageAware, options, new SimpleImageLoadingListener());
return workingview;
}
Please let me know where I'm going wrong.
There is a limit of chache in android around 1MB of image cashing it clears cache and download again that is why it reloads again images.
Its is proper way if you are not downloading images in your SD card of phone memory.
You can not hold much data in caching.
I have a list view and I want to show thumbnail of video in it but everytime I scroll images download again. how can I download images once and show it.
this is my code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
rootView = mInflater.inflate(R.layout.adapter_video, parent, false);
holder = new ViewHolder();
holder.ivThumbnail = (ImageView) rootView.findViewById(R.id.ivThumbnailVideoAdapter);
rootView.setTag(holder);
}
else{
rootView = convertView;
holder = (ViewHolder)rootView.getTag();
}
imageLoader.displayImage(update.get(position).getThumbnail(), holder.ivThumbnail, option);
return rootView;
}
with UIL the download of the images is configured with DisplayImageOptions
For example :
mOptionsSimple = new DisplayImageOptions.Builder().resetViewBeforeLoading(true)
.cacheOnDisc(true)
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
Here, cacheOnDisc to true saves the image on the disk, not in the cache. When you scroll the image will reload, of course because in listview cells are recycled. But with this option it will reload from the disk if it was already downloaded
You can also/or enable memoryCache BUT be carefull with this one. It can be the cause of OutOfMemoryException if the images are not managed correclty
also ImageLoaderConfiguration here is an example :
config = new ImageLoaderConfiguration.Builder(getApplicationContext()).threadPoolSize(3)
.threadPriority(Thread.NORM_PRIORITY - 1)
.tasksProcessingOrder(QueueProcessingType.FIFO)
.imageDownloader(new MyImageDownloader(mContext))
.imageDecoder(new BaseImageDecoder(false))
.discCache(new UnlimitedDiscCache(cacheDir))
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.defaultDisplayImageOptions(mOptionsSimple)
.writeDebugLogs()
.build();
In this last example the discCache is not limited which could be changed if you think it ll take too much space on the disk
I have an android game application in which each level has a 250*250(pixel) image and all the levels are in one Activity named "game1.java". because many layout features change in the levels; I call the same class using Intent and finish() the class when I wanna change the level of the game.
in that I have problem in the ram usage and I tried to use this in the game1 class:
https://github.com/nostra13/Android-Universal-Image-Loader
but I get Unfortunately each time I wanna open the activity.
this is an attribute of the game1 class:
ImageLoader imageLoader;
this part is at the start of onCreate:
super.onCreate(savedInstanceState);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();
ImageLoader.getInstance().init(config);
File cacheDir = StorageUtils.getCacheDirectory(getBaseContext());
DisplayImageOptions options = new DisplayImageOptions.Builder()
.resetViewBeforeLoading(false) // default
.delayBeforeLoading(0)
.cacheInMemory(false) // default
.cacheOnDisk(true) // default
.build();
whenever I need a new image i call this function and give the strName name and extension of my image that is in assets folder. for example "image1.jpg"
private Bitmap getBitmapFromAsset(String strName)
{
String imageUri = "assets://" + strName;
//Bitmap bmp = ImageLoader.getInstance().loadImageSync(imageUri);
imageLoader.loadImage(imageUri,new SimpleImageLoadingListener(){
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
bmp_loader=loadedImage;
}
});
return bmp_loader;
}
then I give the image to the ImageView via setImageBitmap function
where is the problem?
using my friends' helps; this is what i did eventually to fix the problem:
step 1: I setup my loader in the first activity of my game; in the onCreate:
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration
.Builder(getBaseContext())
.defaultDisplayImageOptions(defaultOptions)
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
.discCacheSize(50 * 1024 * 1024)
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.build();
step 2:
in the game1.class ; after super.oncreate:
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();
ImageLoader.getInstance().init(config);
File cacheDir = StorageUtils.getCacheDirectory(getBaseContext());
DisplayImageOptions options = new DisplayImageOptions.Builder()
.resetViewBeforeLoading(false) // default
.delayBeforeLoading(0)
.cacheInMemory(false) // default
.cacheOnDisk(true) // default
.build();
I gave the imageaddress that needed to a string
ax_level = "level_01.jpg";
defined my imageview
ImageView posht_safhe = (ImageView) findViewById(R.id.rook_ax);
gave the image to my imageView
String pin_url = "assets://"+ax_level;
DisplayImageOptions option = new DisplayImageOptions.Builder()
.cacheOnDisc(true)
.cacheInMemory(false )
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageLoader.getInstance().displayImage(pin_url ,posht_safhe, option, new SimpleImageLoadingListener() {
#Override
public void onLoadingComplete(String imageUri, View view,
Bitmap loadedImage) {
// bitmap = loadedImage;
}
});
and finally it worked
this is very simple mistake :)
you didn't define imageLoader and the imageLoader is null when you try to call loadImage(...)!
change your code ImageLoader.getInstance().init(config); to this
imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
I am using Universal ImageLoader for downloading large images from server and displaying them in coverflow. Issue I am facing here is that I am unable to set custom size for my image. It just takes whole screen. For example, I am downloading image of size 500 x 1200 - and I want to display it of size 300 x 300. But it takes full display. Please help me with this issue as I am stuck for more than 3 days. Thanks.
Code
public class HomeCoverFlow {
Context mContext;
public CoverFlow coverFlow;
ImageAdapter coverImageAdapter;
ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options;
/** Called when the activity is first created. */
public HomeCoverFlow(Context context)
{
mContext = context;
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.flyer_placeholder)
.showImageForEmptyUri(R.drawable.flyer_placeholder)
.imageScaleType(ImageScaleType.EXACTLY)
.cacheInMemory(true)
.build();
coverFlow = new CoverFlow(context);
coverImageAdapter = new ImageAdapter(mContext);
coverFlow.setAdapter(coverImageAdapter);
coverFlow.setSpacing(-25);
coverFlow.setSelection(2, true);
coverFlow.setAnimationDuration(1000);
coverFlow.setGravity(Gravity.CENTER_VERTICAL);
// HEIGHT
coverImageAdapter.coverflowHeight = 100; //display.getHeight() / 3;
//WIDTH
coverImageAdapter.coverflowWidth = 100;// display.getWidth() / 2;
}
A method in adapter to create item views, Here I use imageloader to download image and display
public View getView(int position, View convertView, ViewGroup parent) {
FeaturedFlyerData data = (FeaturedFlyerData) getItem(position);
if(data.flyerImage == null)
{
setNewImage(position);
}
//see text or image
if(data.displayImage)
{
data.flyerImage.setBackgroundColor(Color.BLACK);
imageLoader.displayImage(data.url, data.flyerImage, options, null);
}
return data.flyerImage;
}
void setNewImage(int position)
{
FeaturedFlyerData data = (FeaturedFlyerData) getItem(position);
data.flyerImage = new ImageView(mContext);
data.flyerImage.layout(0, 10, 200,350);
data.flyerImage.setScaleType(ScaleType.CENTER_INSIDE);
data.flyerImage.setTag(Integer.toString(position));
data.flyerImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
imageClicked(Integer.parseInt(v.getTag().toString()));
}
});
}
}
I don't know what else code snippet to provide here. Please comment if you need any further clarifications. Thanks.
I am using following code to display custom size images in coverflow using universal imageloader.
In constructor, set DisplayImageOptions as:
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.flyer_placeholder)
.showImageForEmptyUri(R.drawable.flyer_placeholder)
.imageScaleType(ImageScaleType.EXACTLY)
.cacheInMemory(true)
.build();
//set coverflow full screen, and images size.
coverFlow.setSpacing(-25);
coverFlow.setAnimationDuration(1000);
coverFlow.setGravity(Gravity.CENTER_VERTICAL);
coverFlow.setLayoutParams(new CoverFlow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
// GET SCREEN SIZE
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
//Here I am setting image size using my custom adapter in "dp". Avoid to use pixels as things will mess up for different screen sizes
coverImageAdapter.coverflowWidth = (int) Math.ceil(dm.widthPixels * (dm.densityDpi / 160.0));
coverImageAdapter.coverflowHeight = (int) Math.ceil(dm.heightPixels * (dm.densityDpi / 160.0)) / 2;
In adapter's setNewImage method (called in getView), use Gallery.LayoutParams:
void setNewImage(int position)
{
FeaturedFlyerData data = (FeaturedFlyerData) getItem(position);
data.flyerImage = new ImageView(mContext);
data.flyerImage.setLayoutParams(new Gallery.LayoutParams(coverflowWidth, coverflowHeight));
data.flyerImage.setScaleType(ScaleType.CENTER_INSIDE);
}
I am trying to use 'Universal Image Loader' to load images inside of my ListView. My code looks like that:
public View getView(int position, View convertView, ViewGroup parent)
if (convertView == null) {
convertView = mInflater.inflate(R.layout.offers_list_adapter, null);
holder = new ViewHolder();
holder.mIvImage = (ImageView) convertView
.findViewById(R.id.ivImage);
holder.mTxtName = (TextView) convertView.findViewById(R.id.txtName);
holder.mTxtCategoryName = (TextView) convertView
.findViewById(R.id.txtCategoryName);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
I have one rule:
If this item in my list is special one, I have to change my ImageView's params (e.g. special item has imageView bigger than normal).
So in my code I do that:
if (holder.isSpecial) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, 50);
layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
holder.mIvImage.setLayoutParams(layoutParams);
}
After I change my params, I call the 'Universal Image Loader' library as usual:
imageLoader.displayImage(holder.url, holder.mIvImage);
But when the library execute in my UI thread to put the image in my ImageView, my ImageView is not at center and the size is wrong. My changes that I made because this item is special are not applied.
I have my own ImageDownloader and when I use it the ImageView load with the new params correctly (center and new size), but in many other points my own class is not so good as this library.
Anyone have any idea what might be happening?
Thanks in advance,
Cheers!
My 'Universal Image Loader' configuration:
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory().cacheOnDisc()
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
.displayer(new RoundedBitmapDisplayer(0)).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
mContext)
.memoryCacheExtraOptions(480, 800)
.discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75)
.threadPoolSize(3)
.threadPriority(Thread.NORM_PRIORITY - 1)
.denyCacheImageMultipleSizesInMemory()
.offOutOfMemoryHandling()
.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
.discCache(new UnlimitedDiscCache(file))
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.imageDownloader(
new URLConnectionImageDownloader(5 * 1000, 20 * 1000))
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.enableLogging().defaultDisplayImageOptions(options).build();
imageLoader.init(config);
You have copy-pasted configuration from GitHub Readme. This config (on GitHub) just shows ALL possible options, but you SHOULD NOT call all of them.
You should look into example project (UniversalImageLoaderExample) to see right way of tuning and using UIL.