0

尝试从服务器加载和显示图像。

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;
}

但位图始终为空!!!我做错了什么?URL 很好,它可以在浏览器和 iOS 上运行。

编辑1:

当我在浏览器中打开 url 时,它向我显示了类似的内容:

/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAYEBQYFBAYGBQYHBwYIChAKCgkJChQO DwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSgBBwcHCggK EwoKEygaFhooKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgo KCgoKCgoKCgoKP/AABEIAZABLAMBEQACEQEDEQH/xAGiAAABBQEBAQEBAQAAAAAA AAAAAQIDBAUGBwgJCgsQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQci cRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElK U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeo qaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5 +gEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoLEQACAQIEBAMEBwUEBAABAncA AQIDEQQFITEGEkFRB2FxEyIygQgUQpGhscEJIzNS8BVictEKFiQ04SXxFxgZGiYn 

那是什么 ?)

4

1 回答 1

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());

这应该有效。

于 2012-12-10T09:02:28.317 回答