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

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) {
}
});

Related

How to get image size if I know its URL? Android (java)

I use Jsoup to read the website. But in html not all images have size information. So, if it is not there, I want to find out the image size in some other way, using the image source URL.
I would suggest use glide
dependencies {
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
}
and then load your image url using glide
Glide.with(this)
.asBitmap()
.load(path)
.into(new CustomTarget<Bitmap>() {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
int width = bitmap.getWidth();
int height = bitmap.getHeight()
}
#Override
public void onLoadCleared(#Nullable Drawable placeholder) {
}
});
/*this library*/
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
/* this code */
Glide.with(this)
.asFile() // get size image url
.load("link image url")
.into(new SimpleTarget<File>() {
#Override
public void onResourceReady(#NonNull File resource, #Nullable
Transition<? super File> transition) {
fab_full.setLabelText(""+resource.length()); // /1024 kb
}
});

Picasso keeping the old image until new image loaded

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() {
}
});

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");
}
});

Persistent Thumbnails with PIcasso

I'm building a profile page for my app. I've made it in such a way that tapping on the previous profile image, the user will be able to change its profile picture. I would like that after the upload, the profile picture will be instantaneously updated. I've tried using Picasso but it seems to have some problems with the cache. In fact after the user has chosen his image, the picture which is shown is the same as before, despite the fact that the app overwrites the previous image file and re-apply Picasso. I'm using Android API 22.
Profile.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
ImageView pic = (ImageView) findViewById(R.id.picc);
...
String root = Environment.getExternalStorageDirectory().toString();
String path = root + "/directory/name.jpg";
MainActivity.trimCache(this);
Picasso.with(getApplicationContext()).load(path)
.networkPolicy(NetworkPolicy.NO_CACHE)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.transform(new RoundedTransformation(1000, 0))
.resize(500, 500)
.centerCrop()
.into(pic);
}
pic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent chooseImageIntent = ImagePicker.getPickImageIntent(getApplicationContext());
startActivityForResult(chooseImageIntent, PICK_IMAGE_ID);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap bitmap = ImagePicker.getImageFromResult(this, resultCode, data);
ImageView pic = (ImageView) findViewById(R.id.picc);
RetrieveFeedTask job = new RetrieveFeedTask(data, resultCode, this, bitmap);
job.execute("user","pass");
String root = Environment.getExternalStorageDirectory().toString();
String path = root + "/directory/name.jpg";
MainActivity.trimCache(this);
Picasso.with(getApplicationContext()).load(path)
.networkPolicy(NetworkPolicy.NO_CACHE)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.transform(new RoundedTransformation(1000, 0))
.resize(500, 500)
.centerCrop()
.into(pic);
}
ImagePicker.java is a standard image picker file.
I've also tried to delete the cache from the app with the following function
public static void trimCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {
// TODO: handle exception
}
}
The Picasso instance, by default, holds a memory cache. Picasso sees the same key for the cache (the path in this case) and just simply returns the Bitmap instance from the cache that it gets from that key.
You have the right thing there with memoryPolicy(MemoryPolicy.NO_CACHE) to skip the cache check; are you sure that's the way you are calling for the image after the change/update?

Adding overlay image in ImageView

I am using picasso library to load images from url to ImageView,
Picasso.with(activityContext).load(thumbnailurl)
.into(imageViewReference);
Can I set background image of ImageView (Not the source Image), using Picasso.
You can use callback of picasso.However you can't use setBackgroundResource because it's always takes int as parameter and you will get bitmap in return from picasso.
Picasso.with(getContext()).load(url).into(new Target() {
#Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Drawable d = new BitmapDrawable(getResources(),bitmap);
imageView.setBackground(d);
}
#Override public void onBitmapFailed(Drawable errorDrawable) { }
#Override public void onPrepareLoad(Drawable placeHolderDrawable) { }
});
private Bitmap overlay(Bitmap bitmap1, Bitmap bitmap2) {
Bitmap bmOverlay = Bitmap.createBitmap(bitmap1.getWidth(), bitmap1.getHeight(), bitmap1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bitmap1, new Matrix(), null);
canvas.drawBitmap(bitmap2, new Matrix(), null);
return bmOverlay;
}
if you have to set one image over another the same task i have done by using transparent image from drawable and another one is getting from sdcard,using this code it works,you have to just pass your images bitmap in this method()...

Categories