imageView = (ImageView) this.findViewById(R.id.imageview);
imageView1 = (ImageView) this.findViewById(R.id.imageview1);
Bitmap bitmap = null;
try {
bitmap = getImage("http://10.0.2.2:8080/myweb/photo/logo.png");
imageView.setImageBitmap(bitmap);
bitmap = getImage("http://10.0.2.2:8080/myweb/photo/logo1.png");
imageView1.setImageBitmap(bitmap);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
private Bitmap getImage(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
byte[] data;
con.setRequestMethod("GET");
if (con.getResponseCode() == 200) {
InputStream in = con.getInputStream();
data = read2Byte(in);
return BitmapFactory.decodeByteArray(data, 0, data.length);
} else
return null;
}
private byte[] read2Byte(InputStream in) throws IOException {
byte[] data;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) != -1) {
bout.write(buf, 0, len);
}
data = bout.toByteArray();
return data;
}
}
|
|