Reputation: 89
I am using view pager in my android app to show images which automatically scroll after a particular time.
In my OnCreate I have defined the pager like this:
List<String> urls;
urls= new ArrayList<>();
urls.add("http://<myserver>/adrotatecustom1.png");
urls.add("http://<myserver>/adrotatecustom2.jpg");
urls.add("http://<myserver>/adrotatecustom3.jpg");
urls.add("http://<myserver>/adrotatecustom4.jpg");
mImageViewPager.setAdapter(new CustomPagerAdapter(getApplicationContext(), urls));
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabDots);
tabLayout.setupWithViewPager(mImageViewPager, true);
autoSlider(mImageViewPager);
Autoslider method:
public void autoSlider(final ViewPager viewPager)
{
final Handler handler = new Handler();
final Runnable rr = new Runnable()
{
public void run()
{
int i=0;
int pos = viewPager.getCurrentItem();
if(pos >= i && pos != mImages.length - 1){
i = pos;
i++;
}
else if(pos < (i-1)){
i = pos;
i++;
}
viewPager.setCurrentItem(i, true);
i++;
if (i >= mImages.length)
i = 0;
autoSlider(viewPager);
}};
handler.postDelayed(rr, 15000);
}
Adapter for View Pager:
public class CustomPagerAdapter extends PagerAdapter {
Context c;
private List<String> _imagePaths;
private LayoutInflater inflater;
public CustomPagerAdapter(Context c, List<String> imagePaths) {
this._imagePaths = imagePaths;
this.c = c;
}
@Override
public int getCount() {
return this._imagePaths.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == (object);
}
@Override
public Object instantiateItem(ViewGroup container,final int position) {
ImageView imgDisplay;
inflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.activity_viewpagerlayout, container,
false);
imgDisplay = (ImageView) viewLayout.findViewById(R.id.imgViewpager);
Picasso.with(c).load(_imagePaths.get(position)).into(imgDisplay);
(container).addView(viewLayout);
return viewLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
(container).removeView((LinearLayout) object);
}
}
The problem is it working fine initially, if i change the image in server(different image with the same name), it is still referring to the old image, but i am not having the old image anywhere in the server. Can anyone help me what i am missing here.
Upvotes: 1
Views: 334
Reputation: 69
I used Glide for image loading and used .diskCacheStrategy(DiskCacheStrategy.NONE)
Here is my method:
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imgDisplay;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.viewpager_image, container,
false);
imgDisplay = viewLayout.findViewById(R.id.image);
StorageReference storageReference = FirebaseStorage.getInstance().getReferenceFromUrl(_imagePaths.get(position));
Glide.with(context).load(storageReference)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.placeholder(R.drawable.avator)
.error(R.drawable.avator)
.into(imgDisplay);
(container).addView(viewLayout);
return viewLayout;
}
Upvotes: 0
Reputation: 3627
its displaying old images because the URL is same while loading images and the library like Picaso/Glide
is load image from cashe first. so if you want to load newly uploaded image then append timestamp
in your URL
and ImageLoading library autmaticaly load new image.
e.g. url="http://abc.co/img.jpg?2017-12-31 11:12:13";
in your case update your all imagepaths
Upvotes: 0
Reputation: 3841
if i change the image in server(different image with the same name), it is still referring to the old image
ViewPager
not refreshing in android
Don't use same name with different image because when images are load at first time via Picasso
Lib it will store in CASH
memory and if from server side you will change image with old same name Picasso
will predict it is a same image and display or load a old image not new one
So my suggestion is: While updating images from your server you should have to store updated image name not same as previous.
if you still want to use same name you can use .memoryPolicy(MemoryPolicy.NO_CACHE)
it will remove all CACHE
memory form your application used by Picasso
Example
Picasso.with(context)
.load(listView.get(position))
.memoryPolicy(MemoryPolicy.NO_CACHE)
.networkpolicy(NetworkPolicy.NO_CACHE)
.into(imageView);
For more information official DOC and memory-cache-Piccaso MemoryPolicy class
Upvotes: 0
Reputation: 309
use glide library for image loading :
Glide.with(holder.itemView.getContext()).load(_imagePaths.get(position)).
diskCacheStrategy(DiskCacheStrategy.RESULT).
placeholder(R.color.off_white).into(holder.imgImage);
in build.gradle
compile 'com.github.bumptech.glide:glide:3.7.0'
Upvotes: 0
Reputation: 9692
if i change the image in server(different image with the same name), it is still referring to the old image
you need to clear cache of Picasso
use this .memoryPolicy(MemoryPolicy.NO_CACHE)
and .networkpolicy(NetworkPolicy.NO_CACHE)
to clear cache of Picasso
CODE
Picasso.with(c)
.load(_imagePaths.get(position))
.memoryPolicy(MemoryPolicy.NO_CACHE)
.networkpolicy(NetworkPolicy.NO_CACHE)
.into(imgDisplay);
Upvotes: 1