donglulong0877 2014-09-04 19:42
浏览 99
已采纳

Android从Web服务器获取图像到ImageView而不是缓存副本

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);
              }
            }

    }
  • 写回答

1条回答 默认 最新

  • doukan6564 2014-09-04 19:52
    关注

    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());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?