imageview显示服务器图片,imageview 直接加载图片流

imageview 直接加载图片流

第一步:new一个线程直接获取接口返回的图片流stream,如果项目使用的网络请求框架能直接获取inputstream,那就不需要自己写线程。

/**

* 获取网络图片资源

*

* @param urlString

* @return

*/

public void getHttpBitmap(final String urlString) {

new Thread(new Runnable() {

@Override

public void run() {

HttpURLConnection connection = null;

try {

URL url = new URL(urlString);

connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("GET");

connection.setConnectTimeout(8000);

connection.setReadTimeout(8000);

Bitmap bitmap = null;

final InputStream is = connection.getInputStream();

if (is == null) {

throw new RuntimeException("stream is null");

} else {

try {

byte[] data = readStream(is);

if (data != null) {

bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

}

} catch (Exception e) {

e.printStackTrace();

}

is.close();

}

// // 下面对获取到的输入流进行读取

// BufferedReader reader = new BufferedReader(new InputStreamReader(in));

// StringBuilder response = new StringBuilder();

// String line;

// while ((line = reader.readLine()) != null) {

// response.append(line);

// }

Message message = new Message();

message.what = SHOW_RESPONSE;

// 将服务器返回的结果存放到Message中

message.obj = bitmap;

handler.sendMessage(message);

} catch (Exception e) {

e.printStackTrace();

} finally {

if (connection != null) {

connection.disconnect();

}

}

}

}).start();

}

第二步:

如果直接使用

BitmapFactory.decodeStream(inputStream),这个时候得到的是null。

原因:应该是decodeStream的时候流已经关闭。因为我是把inputstream放在message中,传递到handle中,再解析流。

参考:http://blog.csdn.net/maxwell_nc/article/details/49081105

/*

* 得到图片字节流 数组大小

* */

public static byte[] readStream(InputStream inStream) throws Exception {

ByteArrayOutputStream outStream = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];

int len = 0;

while ((len = inStream.read(buffer)) != -1) {

outStream.write(buffer, 0, len);

}

outStream.close();

inStream.close();

return outStream.toByteArray();

}

第三步:

在handle中接收处理

public final static int SHOW_RESPONSE = 110;

private Handler handler = new Handler() {

public void handleMessage(Message msg) {

switch (msg.what) {

case SHOW_RESPONSE:

//然后使用方法decodeByteArray()方法解析编码,生成Bitmap对象。

mIvCaptcha.setImageBitmap((Bitmap) msg.obj);

}

}

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在Android应用程序中将服务器图片加载ImageView,你可以使用以下步骤: 1. 从服务器下载图片:你可以使用网络请求(例如使用HTTP请求或Volley库)从服务器下载图片数据。 2. 将图片数据转换为Bitmap:你可以使用Bitmap工厂类(例如BitmapFactory)将图片数据转换为Bitmap。 3. 设置Bitmap到ImageView:最后,使用ImageView的setImageBitmap()方法将Bitmap设置到ImageView。 例如: ``` ImageView imageView; String imageUrl = "http://example.com/image.png"; //使用Volley请求图像 ImageRequest request = new ImageRequest(imageUrl, new Response.Listener<Bitmap>() { @Override public void onResponse(Bitmap bitmap) { imageView.setImageBitmap(bitmap); } }, 0, 0, null, new Response.ErrorListener() { public void onErrorResponse(VolleyError error) { imageView.setImageResource(R.drawable.default_image); } }); //将请求添加到请求队列 RequestQueue requestQueue = Volley.newRequestQueue(this); requestQueue.add(request); ``` ### 回答2: 在Android中,要将服务器上的图片加载ImageView上,可以通过使用网络请求库和图片加载库来实现。下面是一种常用的方法: 首先,需要使用网络请求库发送请求获取服务器上的图片资源。常用的网络请求库有OkHttp和Retrofit,可以根据个人习惯选择适合自己的库。 接下来,需要使用图片加载库将获取到的图片资源加载ImageView上。常用的图片加载库有Glide和Picasso,具体选择可以根据项目需求进行判断。 首先,导入相应的网络请求库和图片加载库的依赖,可以在项目的build.gradle文件中添加相应的依赖。 接下来,在Activity或Fragment中,通过网络请求库发送请求获取服务器上的图片资源。可以使用网络请求库提供的方法发送GET请求,并将获取到的图片资源保存在本地。 最后,在获取到图片后,使用图片加载加载图片资源到ImageView上。可以通过图片加载库提供的方法将图片资源加载ImageView中。 以下是示例代码: ```java // 使用OkHttp发送网络请求 OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("图片的URL地址") .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { // 获取到图片的字节 InputStream inputStream = response.body().byteStream(); // 使用图片加载加载图片资源到ImageView上 runOnUiThread(() -> { Glide.with(this) .load(inputStream) .into(imageView); }); } }); ``` 需要注意的是,以上代码仅为示例,具体使用时,需要根据实际情况进行适当的修改。另外,还需添加网络权限和相关代码,保证网络请求能够正常运行。 通过以上步骤,就可以将服务器上的图片加载ImageView上了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值