目前我正在尝试下载存储在我的服务器目录中的图像。但我不知道为什么我的应用程序仍然显示该图像,即使该图像被另一个图像替换。
这意味着首先我上传image1,然后我可以加载image1。当 image1 被 image2 替换时,应用程序仍然显示 image1。
我不确定我的错误在哪里。是代码还是其他原因。需要一些帮助!
下面是我的代码:
button1.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
new download().execute();
}
});
class download extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... path) {
String outPut = null;
String s = "http://url/image/img_123.jpg";
URL myFileUrl = null;
try {
myFileUrl = new URL(s);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
int length = conn.getContentLength();
int[] bitmapData = new int[length];
byte[] bitmapData2 = new byte[length];
InputStream is = conn.getInputStream();
bm = BitmapFactory.decodeStream(is);
outPut = "success";
} catch (IOException e) {
e.printStackTrace();
}
return outPut;
}
protected void onPostExecute(String file_url) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
imageView1.setImageBitmap(bm);
}
});
}
}
更新
button1.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// new download().execute();
Drawable drawable = LoadImageFromWeb("http://url/image/img_123.jpg");
imageView1.setImageDrawable(drawable);
}
});
private Drawable LoadImageFromWeb(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}