android开发-通过HttpURLConnection获取网络内容,Bitmap把二进制格式的图片转为位图

本文介绍如何使用Android的HttpURLConnection从网络获取图片,并将其转换为Bitmap显示在ImageView中。包括XML布局配置、Internet权限设置、图片获取和服务端口的实现。

android开发-通过HttpURLConnection获取网络内容,

Bitmap把二进制格式的图片转为位图

布局:用ImageView显示图片

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <EditText
        android:id="@+id/editTest_urlpath"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
      android:text="http://pic3.nipic.com/20090630/2987337_202447057_2.jpg">
    </EditText>
    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="获取图片"/>
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

权限:

要从网上获取到图片,或者其他内容,必须先弄个internet权限

<uses-permission android:name="android.permission.INTERNET"/>

Activity:测试类,把获取到的二进制图片转为位图并显示出来

@Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_image_view);
      
       editText_urlpath = (EditText) this.findViewById(R.id.editTest_urlpath);
       button = (Button) this.findViewById(R.id.button);
       imgView = (ImageView) this.findViewById(R.id.imageView);
      
       button.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
              byte[] data = ImageViewService.getImage();
              //把二进制图片转成位图
              Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
              imgView.setImageBitmap(bitmap);
           }
       });
    }

Service:提供从网上获取图片的方法(只是测试随便,什么参数得自己去提取了)

public class ImageViewService {
    public static byte[] getImage() {
       byte[] data = null;
       try {
           //建立URL
           URL url = new URL("http://pic3.nipic.com/20090630/2987337_202447057_2.jpg");
           HttpURLConnection conn = (HttpURLConnection) url.openConnection();
           conn.setRequestMethod("GET");
           conn.setReadTimeout(5000);
          
           InputStream input = conn.getInputStream();
           data = Util.readInputStream(input);
           input.close();
          
           System.out.println("下载完毕!");
       } catch (MalformedURLException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
       return data;
    }
}

Util:工具类

public class Util {
    public static byte[] readInputStream(InputStream input) {
       ByteArrayOutputStream output = new ByteArrayOutputStream();
       try {
           byte[] buffer = new byte[1024];
           int len = 0;
           while((len = input.read(buffer)) != -1) {
              output.write(buffer, 0, len);
           }
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
       return output.toByteArray();
    }
}



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值