1

this is the code I'm using.

link contain image http://mycapturetest.byethost17.com/Screen.jpg

when i update image on URl and open it on browser. it shows updated image.

but when open the same link in android application it keep showing the last image uploaded on the URl. (Not the new one)

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;



    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
        bmImage.setImageDrawable(null);
        bmImage.setImageBitmap(null);
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {

               bmImage.setImageDrawable(null);
               bmImage.setImageBitmap(null);
               bmImage.setImageBitmap(result);
               Toast.makeText(MainActivity.this, "set....", Toast.LENGTH_SHORT).show();


    }
}
Marcus
  • 6,697
  • 11
  • 46
  • 89
Aater Ali
  • 11
  • 3
  • m calling function in oncreate() method like new DownloadImageTask((ImageView) findViewById(R.id.imageView1)) .execute("http://mycapturetest.byethost17.com/Screen.jpg"); – Aater Ali Mar 05 '15 at 08:25
  • I suggest you to use this library https://github.com/nostra13/Android-Universal-Image-Loader . It's the best to me. – Atlas91 Mar 05 '15 at 08:29
  • any solution with out using library ? – Aater Ali Mar 05 '15 at 08:37
  • the problem seems is the caching..but if you use that library you will not have any problem. Trust me. it's easy to use and the best way. You can found several tutorials about that. – Atlas91 Mar 05 '15 at 08:41
  • its working but how to do it repeatedly like i wanna update image after 5 seconds – Aater Ali Mar 05 '15 at 09:26
  • create a timer in your task that runs every 5 seconds – Atlas91 Mar 05 '15 at 09:30
  • using ImageLoader imageLoader = ImageLoader.getInstance(); imageLoader.displayImage(url, img,defaultOptions); still getting old image – Aater Ali Mar 05 '15 at 10:27

1 Answers1

0

You can turn off caching of resources loaded from URLs using the URLConnection class:

    try {
        URLConnection connection = new java.net.URL(urldisplay).openConnection();
        connection.setUseCaches(false);
        InputStream in = connection.getInputStream();
        mIcon11 = BitmapFactory.decodeStream(in);
    } catch (Exception e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
samgak
  • 23,944
  • 4
  • 60
  • 82
  • its working but how to do it repeatedly like i wanna update image after 5 seconds – Aater Ali Mar 05 '15 at 09:26
  • schedule your DownloadImageTask to run at repeat intervals. see the answer to this question for how to do it: http://stackoverflow.com/questions/6531950/how-to-execute-async-task-repeatedly-after-fixed-time-intervals – samgak Mar 05 '15 at 09:29