简体   繁体   中英

Android get image to ImageView from web server not cashed copy

part of my android application display image on ImageView from web server(php). the name of the image on the server has the same IMEI phone number so that the phone would recognize which image belong to it

example Imei is 33245576544535 then the image is www.example.uploads/33245576544535.jpg

the problem is when the image change but still the same name the phone keep getting the old cashed copy of the image.

how to get always the image from my server and not from cash server. here is the action code and thank you in advance

package com.sunil.upload;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

    public class see extends Activity{

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.look);
            String  phoneNo;
             TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                phoneNo=telephonyManager.getDeviceId()+"";
            new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
            .execute("http://example.com/uploads/"+phoneNo+".jpg");
        }


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

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

              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.setImageBitmap(result);
              }
            }

    }

Adding an unique parameter to the url makes the cache to always miss:

    urldisplay = urldisplay + “?timestamp=“ + System.currentTimeMillis();
    InputStream in = new java.net.URL(urldisplay).openStream();

But consider using some other identifier than the IMEI. It's bad for user privacy to collect IMEI and will most likely not work if more than one user uses your app with the same device.

If adding the parameter is not possible, you can try adding “Cache-Control: no-cache” and “Pragma: no-cache” headers to your request:

    URL url = new URL(urldisplay);
    URLConnection urlConnection = url.openConnection();
    urlConnection.setRequestProperty("Cache-Control", "no-cache");
    urlConnection.setRequestProperty(“Pragma”, "no-cache");
    InputStream in = new BufferedInputStream(urlConnection.getInputStream());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM