android对图片进行处理的类为Bitmap ,Bitmap 位图比较特别 需要借助于BitmapFactory来创建
BitmapFactory,Creates Bitmap objects from various sources, including files, streams, and byte-arrays
这句话充分说明了BitmapFactory可以处理图片的几种来源
使用streams处理最为简单
URL url = new URL(imagePath);//将一个string包装成URL
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //这里返回一个URLConnection,将其转换为HttpURLConnection
// conn.setDoInput(true); 允许输入数据
conn.connect(); //连接
InputStream in = conn.getInputStream(); //通过连接得到输入流
BufferedInputStream bis = new BufferedInputStream(in); //将输入流包装成拥有内部缓冲区BufferedInputStream
Bitmap bmp = BitmapFactory.decodeStream(bis); // BitmapFactory从BufferedInputStream流中读取图片数据
iv.setImageBitmap(bmp); // ImageView中显示,也有将Bitmap转为BitMapDrawable用setImageDrawable设置的
bis.close();
in.close();
HttpURL