Profile Image Uploader and Load it with Picasso

自作多情 提交于 2020-01-07 02:56:48

问题


I have an android app which has profile image. The user can edit his/her profile image. I'm using Picasso to load images, but every time I load profile image, Picasso loads old image.

When I upload profile image I don't change it's url, so profile image url for every user is constant.

First question: should I change profile image url every time user change profile image?

Second question: When user change his/her profile image I invalidate Picasso to load new image using this code:

picassoInstance.load(url)
  .networkPolicy(NetworkPolicy.NO_CACHE)
  .transform(new CircleTransform())
  .placeholder(dr)
  .into(imageView);

But Picasso show me the old one. And one more thing: I load image url into imageView without calling networkPolicy method at first. How should I handle this problem?


回答1:


Every Image has different name, whenever new image uploaded you have to update your image Url

Eg. current url : https://photographylife.com/nikon-d810-high-resolution-image-samples

where Image name is : nikon-d810-high-resolution-image-samples

and after uploaded new image, name will be different for example : image-sample

so you need to update that image Url : https://photographylife.com/image-sample with new name

try this, i telling you cause im doing the same hope this will help you!




回答2:


So lets just discuss one by one:

Should I change profile image url every time user change profile image?

No. You might think of other ways around. In my case, I download the picture each time the url changes with a specific name of the user (as we're considering profile picture). So when I loaded the image with Picasso I passed the file path instead of the URL so that, it won't fetch from the URL each time. Picasso doesn't do that either actually. It maintains a cache. If the file doesn't exist, just put a placeholder.

When user change his/her profile image I invalidate Picasso to load new image using this code:

I don't see any invalidate code here in your code. I would refer to this link to see how they've solved the cache problem.

I load image url into imageView without calling networkPolicy method at first.

So if you fetch the image from the external storage, I don't think there's a necessity to do so.

And another suggestion is to use Glide in these cases. This is almost the same thing as Picasso, but personally I like to use Glide instead of Picasso.

Hope that helps.




回答3:


Using Glide instead of Picasso because Glide recommended by google http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en

Preventing for caching in Glide

Glide.with(DemoActivity.this)
    .load(Uri.parse("file://" + imagePath))
    .diskCacheStrategy(DiskCacheStrategy.NONE)
    .skipMemoryCache(true)
    .into(mImage);



回答4:


Add this class to the com.squareup.picasso package.

package com.squareup.picasso;

public class PicassoTools {

    public static void clearCache (Picasso p) {
        p.cache.clear();
    }
}

Because cache has package visibility, this util class can clear the cache for you. You just have to call it:

PicassoTools.clearCache(Picasso.with(context));



回答5:


I find where problem is: I turned down disk cache but not memory cache. I should use this code:

picassoInstance.load(url)
.networkPolicy(NetworkPolicy.NO_CACHE)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.transform(new CircleTransform())
.placeholder(dr)
.into(imageView);

I use this link to: picasso-influencing-image-caching

Thanks everybody




回答6:


You need to invalidate the file before loading use the code given below:

Picasso.with(getActivity()).invalidate(file);

To know more details kindly study the Picasso documentation from their website.



来源:https://stackoverflow.com/questions/35694055/profile-image-uploader-and-load-it-with-picasso

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!