

Picasso is an excellent library for loading image from URL to your ImageView in Android. Its load image at first time from server and stores in cache and then afterwards every time it loads that image from that cache. if its unable to load from cache, it will again load from server and stores in cache. But there is an issue with Picasso, if you update the image in server it wont recognize that change and still it will load from cache. Picasso said they are working on that feature and will add that to library in future. but till now they didn’t release any update on it.
So i was working on a Project where i need implement this Image update feature but i didn’t find any perfect solution. so the only solutions was to clear cache when we change the image in link so that it will load from server.
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CLEAR_APP_CACHE" />
Build.Gradle
compile 'com.squareup.picasso:picasso:2.5.2' compile 'com.squareup.okhttp:okhttp:2.4.0' compile 'org.apache.httpcomponents:httpcore:4.4.1' compile 'org.apache.httpcomponents:httpclient:4.5'
HttpHandler.Java – For fetching data from a link.
public class HttpHandler { private static final String TAG = HttpHandler.class.getSimpleName(); public HttpHandler() { } public String makeServiceCall(String reqUrl) { String response = null; try { URL url = new URL(reqUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); // read the response InputStream in = new BufferedInputStream(conn.getInputStream()); response = convertStreamToString(in); } catch (MalformedURLException e) { Log.e(TAG, "MalformedURLException: " + e.getMessage()); } catch (ProtocolException e) { Log.e(TAG, "ProtocolException: " + e.getMessage()); } catch (IOException e) { Log.e(TAG, "IOException: " + e.getMessage()); } catch (Exception e) { Log.e(TAG, "Exception: " + e.getMessage()); } return response; } private String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line; try { while ((line = reader.readLine()) != null) { sb.append(line).append('\n'); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } }
MainActivity.Java – Here Setting Picasso view and fetching Json data in background. if the value in data is true it will clear the app cache or else it will do nothing. Replace URLs with yours.
String url = "http://yoururl.com/clearappcache.txt"; //your URL protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageView = (ImageView) findViewById(R.id.imageView); Picasso.with(this) .load("http://yoururl.com/homecover.jpg") //your URL .placeholder(R.drawable.homecover) .error(R.drawable.homecover) .into(imageView); new PostJsonProcess().execute(); } private class PostJsonProcess extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... arg0) { HttpHandler sh = new HttpHandler(); String jsonStr; // Making a request to url and getting response if (haveNetworkConnection()) { jsonStr = sh2.makeServiceCall(url); try { JSONObject jsonObj = new JSONObject(jsonStr); JSONObject check = jsonObj.getJSONObject("check"); clearcache = check.getString("clearcache"); Log.e(TAG, clearcache); } catch (final JSONException e) { Log.e(TAG, "Json parsing error: " + e.getMessage()); } if (clearcache == "true") { clearApplicationData(); } } return null; } private boolean haveNetworkConnection() { boolean haveConnectedWifi = false; boolean haveConnectedMobile = false; ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo[] netInfo = cm.getAllNetworkInfo(); for (NetworkInfo ni : netInfo) { if (ni.getTypeName().equalsIgnoreCase("WIFI")) if (ni.isConnected()) haveConnectedWifi = true; if (ni.getTypeName().equalsIgnoreCase("MOBILE")) if (ni.isConnected()) haveConnectedMobile = true; } return haveConnectedWifi || haveConnectedMobile; } public void clearApplicationData() { File cache = getCacheDir(); File appDir = new File(cache.getParent()); if (appDir.exists()) { String[] children = appDir.list(); for (String s : children) { if (!s.equals("lib")) { deleteDir(new File(appDir, s));Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************"); } } } } public 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(); } }
clearappcache.txt – Upload this txt file in your server and set link to this file in your MainActivity. So when ever you want update image, just upload that and edit this text file and replace “true” instead of “false”. keep it like that for a specific period like a week or month and change it back to “false”. So all phones connected to internet in that specific period of time will get this image update.
{ "check": { "clearcache":"false" } }