Picasso keeping the old image until new image loaded - java

I have a fragment that i'm calling again and again it has Picasso in it to load images from url when I pop back same fragment with different url to load image from, the previous image shown until new image loaded, how can i solve this problem, here is my code:
Picasso.with(context)
.load(url)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(ivParallex, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
getProductDetail(productId);
} else {
}
#Override
public void onError() {
}
});

Related

How to wait Picasso to finish load Images in Android?

So, I have an activity, lets say a PetDetailActivity that shows a carousel with some Bitmaps in it (I use com.synnapps:carouselview:0.1.5 to handle my carousel). The problem is that the PetDetailActivity loaded with 0 sized carousel, which maybe the images is still being processed by a thread. How to wait Picasso to finish processing URLs, and then show it up in the new Activity?
This is the code of PetDetailActivity:
import ...
public class PetDetailActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pet_detail);
Intent i = getIntent();
Pet targetPet = (Pet)i.getSerializableExtra("PetObject");
ActionBar actionBar = getSupportActionBar();
if(actionBar!=null) actionBar.setDisplayHomeAsUpEnabled(true);
//Creating a BitmapHandler object to download image from URL to Bitmap object using picasso.
BitmapHandler bitmapHandler = new BitmapHandler(targetPet.getCarouselImageUrl());
final ArrayList<Bitmap> petCarouselBitmaps = bitmapHandler.getProcessedBitmap();
//The bitmap is being downloaded in other thread, so the activity is up and
//CarouselView is still empty (petCarouselBitmaps.size() == 0)
//So how to wait the bitmaps is processed, like show a loading screen on the UI?
CarouselView petCarousel = findViewById(R.id.petCarousel);
petCarousel.setPageCount(petCarouselBitmaps.size());
petCarousel.setImageListener(new ImageListener() {
#Override
public void setImageForPosition(int position, ImageView imageView) {
imageView.setImageBitmap(petCarouselBitmaps.get(position));
}
});
}
...
}
And Here is the BitmapHandler Class that downloads image from URL to a Bitmap using picasso:
public class BitmapHandler extends Thread {
ArrayList<String> urlList;
private ArrayList<Bitmap> loadedBitmap;
public BitmapHandler(ArrayList<String> list){
this.urlList = list;
this.loadedBitmap = new ArrayList<>();
}
public ArrayList<Bitmap> getProcessedBitmap(){
this.run();
//Returning the loaded bitmap as a ArrayList<Bitmap> Object.
return loadedBitmap;
}
#Override
public void run() {
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
loadedBitmap.add(bitmap);
}
#Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
for (String url : urlList) {
Picasso.get().load(url).into(target);
}
}
}
Thank you for any helps!
Problem: . How to wait Picasso to finish processing URLs
Solution:
I think you can go with Target callback:
private Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
}
#Override
public void onBitmapFailed() {
}
}
And while loading image, you need to write:
Picasso.with(this).load(myURL).into(target);
Just for the information:
onBitmapLoaded() mostly used to perform image operations before we load into the view actually.
Picasso.LoadedFrom describes where the image was loaded from, whether it's MEMORY, DISK or NETWORK.
I think you can use placeholder & then the image is loaded it will show in Image View.
And if you want to delay use can use Thread.sleep(5000).

How to check when Glide removes an image from the cache?

I am using Glide to load images. Currently, when the images are already loaded, they are saved into a cache and then fetched from it when asked for the same image again. But when the cache gets filled, does Glide delete the old files? If so, how do I know when it has done so and to which image?
Glide.with(context)
.asBitmap()
.load(new GlideUrl(url, new LazyHeaders.Builder()
.addHeader("Authorization", "Bearer " + getAuthorizationToken())
.build()))
.into(new CustomTarget<Bitmap>(width, height) {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
imageView.setImageBitmap(resource);
eventEmitter.onPageRender(position);
}
#Override
public void onLoadCleared(#Nullable Drawable placeholder) {
}
});

Why Picasso did not download the image?

I need to set image to my image view
For this reason I use Picasso library
Here is approach how I do this
File image = new File("file:" + path);
Picasso.with(context)
.load(image)
.placeholder(R.drawable.progress_animation)
.error(R.drawable.image_error_404)
.into(iv);
and also I tried the same without prefix file: like here
File image = new File(path);
Picasso.with(context)
.load(image)
.placeholder(R.drawable.progress_animation)
.error(R.drawable.image_error_404)
.into(iv);
But all the time I got image from .error() ,
There is a path with file: prefix - "file:/storage/emulated/0/Android/data/com.fittingroom.newtimezone/files/default/AvatarPackage/DEFAULT_MY_AVATAR/pose1.jpeg"
and there is path witout file: prefix - "/storage/emulated/0/Android/data/com.fittingroom.newtimezone/files/default/AvatarPackage/DEFAULT_MY_AVATAR/pose1.jpeg"
Anyway I got no result
Why picasso doesn't want to set my image
What am I doing wrong?
Your path prefix is incorrect: use file:/// instead of file:
Thanks #CommonsWare I solve my issue such way
.fit()
.centerInside()
And here is my implementation
File image = new File(path);
Picasso picasso = new Picasso.Builder(context)
.listener(new Picasso.Listener() {
#Override public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
Logger.logError("ERROR Download image: ", exception, context);
}
}).build();
picasso
.load(image)
.fit()
.centerInside()
.placeholder(R.drawable.progress_animation)
.error(R.drawable.image_error_404)
.into(iv, new Callback() {
#Override public void onSuccess() {
Logger.logGeneral("image downloaded");
}
#Override public void onError() {
Logger.logGeneral("onError image downloaded");
}
});

Image view reference in adapter using volley android

Hi suppose i am loading image in imageview using volley in adapter. if image loading completed then onResponse(ImageLoader.ImageContainer response, boolean arg1) fn will be called. my problem is that whether i will get the correct reference of the corresponding image view which is loaded inside onResponse ??
i need to change the scale type of image view after image is loaded.
netImageLoader.get(thumbUrl, new ImageLoader.ImageListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("Error", "Image Load Error: " + error.getMessage());
holder.postImage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
holder.postImage.setImageResource(R.drawable.placeholder_video_image);
}
#Override
public void onResponse(ImageLoader.ImageContainer response, boolean arg1) {
if (response.getBitmap() != null) {
// load image into imageview
holder.postImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.postImage.setImageBitmap(response.getBitmap());
}
}
});

Wrong Position Is Returned

This is the strangest thing. I have an Arraylist being called into an imageview. The image returned is in the position it should be when the image is clicked but when I use a Log, the image returned is the next image, instead of the image in the position.
Ion.with(getApplicationContext())
.load(mImages.get(position))
.noCache()
.progressBar(progressBar)
.progressHandler(new ProgressCallback() {
#Override
public void onProgress(long downloaded, long total) {
progressBar.setVisibility(View.VISIBLE);
}
})
.intoImageView(imageView)
.setCallback(new FutureCallback<ImageView>() {
#Override
public void onCompleted(Exception e, ImageView imageView) {
progressBar.setVisibility(View.GONE);
}
});
And the log right underneath:
Log.d("position of url = ", (mImages.get(position));
This is all placed in a PagerAdapter with the position of the viewpager sourced from a gridview. What could be wrong?
I figured this out.
position=viewPager.getCurrentItem();
and it works fine now!

Categories