java 显示bmp图片格式_java-如何在Android中显示bmp图像

本文介绍了一种使用Apache的HttpClient进行网络请求以加载并解析URL中的图片的方法。该方法通过HttpResponse获取输入流,并利用BitmapFactory.Options配置项来指定图片的解析格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

尝试使用apaches网络客户端.这应该工作.让我知道.

public static Bitmap decodeFromUrl(HttpClient client, URL url, Config bitmapCOnfig)

{

HttpResponse response=null;

Bitmap b=null;

InputStream instream=null;

BitmapFactory.Options decodeOptions = new BitmapFactory.Options();

decodeOptions.inPreferredConfig = bitmapCOnfig;

try

{

HttpGet request = new HttpGet(url.toURI());

response = client.execute(request);

if (response.getStatusLine().getStatusCode() != 200)

{

Log.d("Bad response on " + url.toString());

Log.d("http response: " + response.getStatusLine().toString());

return null;

}

BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(response.getEntity());

instream = bufHttpEntity.getContent();

return BitmapFactory.decodeStream(instream, null, decodeOptions);

}

catch (Exception ex)

{

Log.d("error decoding bitmap from:" + url, ex);

if (response != null)

{

Log.d("http status: " + response.getStatusLine().getStatusCode());

}

return null;

}

finally

{

if (instream != null)

{

try {

instream.close();

} catch (IOException e) {

Log.d("error closing stream", e);

}

}

}

}

不要忘记从异步任务中调用此函数.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值