尝试从服务器加载和显示图像。
public static Bitmap loadImageByURL(final String url,
final Context context, final HttpClient httpClient) {
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
data = entity.getContent();
}
}
bitmap = BitmapFactory.decodeStream(data);
} catch (Exception e) {
// TODO: handle exception
}
return bitmap;
}
但是位图总是空的!我做错了什么?网址很好,可以在浏览器和iOS上使用。
编辑1:
当我在浏览器中打开网址时,它显示了类似的内容:
/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAYEBQYFBAYGBQYHBwYIChAKCgkJChQO DwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSgBBwcHCggK EwoKEygaFhooKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgo KCgoKCgoKCgoKP/AABEIAZABLAMBEQACEQEDEQH/xAGiAAABBQEBAQEBAQAAAAAA AAAAAQIDBAUGBwgJCgsQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQci cRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElK U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeo qaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5 +gEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoLEQACAQIEBAMEBwUEBAABAncA AQIDEQQFITEGEkFRB2FxEyIygQgUQpGhscEJIzNS8BVictEKFiQ04SXxFxgZGiYn
那是什么? )
答案 0 :(得分:0)
首先,使用net创建连接。
InputStream iStream=null;
URL imgUrl = new URL(strImageURL);
//strImageUR image url you want to download.
URLConnection urlCon = imgUrl.openConnection();
iStream = urlCon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(iStream,128);
ByteArrayBuffer baf = new ByteArrayBuffer(128);
int current = 0;
while ((current = bis.read()) != -1)
{
baf.append((byte) current);
}
Bitmap bitmap=BitmapFactory.decodeByteArray(
baf.toByteArray(),
0,
baf.toByteArray().length());
这应该有用。