Android:在远程服务器中更新图像后,imageview仍显示旧图像

时间:2016-07-26 17:00:00

标签: android picasso

我的问题是,在我在远程服务器中手动更改图像后,如果我重新运行我的应用程序,android仍会显示旧图像。在我的真实设备中清除CATCHE和DATA,仍显示旧图像。如果我卸载并再次运行它,那么它显示新的图像,远程服务器中的内容。为什么在清理数据和捕获后,它仍然显示旧图片?

public class MainActivity extends AppCompatActivity {

ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageView = (ImageView)findViewById(R.id.imageView);

    Picasso.with(this)
            .load("http://gator4227.hostgator.com/~harakas/images/olaripihlak@hotmail.com.jpg")
            .resize(250,250)
            .centerCrop()
            .into(imageView);
}

我试过onDestroy(),删除catche和数据,但仍然是同样的问题。

 @Override
protected void onDestroy() {
    deleteCache(this);
    clearApplicationData();
    super.onDestroy();
}

public static void deleteCache(Context context) {
    try {
        File dir = context.getCacheDir();
        if (dir != null && dir.isDirectory()) {
            deleteDir(dir);
        }
    } catch (Exception e) {}
}

public static boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }
    return dir.delete();
}

public void clearApplicationData() {

    File cacheDirectory = getCacheDir();
    File applicationDirectory = new File(cacheDirectory.getParent());
    if (applicationDirectory.exists()) {

        String[] fileNames = applicationDirectory.list();

        for (String fileName : fileNames) {

            if (!fileName.equals("lib")) {

                deleteFile(new File(applicationDirectory, fileName));

            }

        }

    }

}

public static boolean deleteFile(File file) {

    boolean deletedAll = true;

    if (file != null) {

        if (file.isDirectory()) {

            String[] children = file.list();

            for (int i = 0; i < children.length; i++) {

                deletedAll = deleteFile(new File(file, children[i])) && deletedAll;

            }

        } else {

            deletedAll = file.delete();

        }

    }

    return deletedAll;

1 个答案:

答案 0 :(得分:0)

Picasso首先尝试从内存缓存中获取图像。如果它在缓存中可用,那么它将显示该缓存。要覆盖它,可以使用MemoryPolicy。

尝试

Picasso.with(this)
        .load("http://gator4227.hostgator.com/~harakas/images/olaripihlak@hotmail.com.jpg")
        .resize(250,250)
        .memoryPolicy(MemoryPolicy.NO_CACHE)
        .centerCrop()
        .into(imageView);