我需要在列表页面中显示来自api的图像列表.为此,我使用了两种方法.
第一种方法:
通过将url转换为字节数组,然后将其转换为位图.请找到以下代码..
URL imageUrl = new URL(url);
URLConnection ucon = imageUrl.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(500);
int current = 0;
while ((current = bis.read()) != -1) {
/* This approach slowdown the process*/
baf.append((byte) current);
}
byte[] img_ary= baf.toByteArray();
Run Code Online (Sandbox Code Playgroud)
将字节数组转换为位图:
ByteArrayInputStream imageStream = new ByteArrayInputStream(
imgUrl);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
Run Code Online (Sandbox Code Playgroud)
第二种方法:
基于高度和宽度的图像缩放
private static final String TAG_iamge = "Image";
private static final int IO_BUFFER_SIZE = 4 * 1024;
public static …Run Code Online (Sandbox Code Playgroud)