图片资源类型转换为bitmap

1、网络图片:
背景:最新的SDK,不允许在main线程里面执行网络操作,否则报错:NetworkOnMainThreadException。
解决:在main线程中另开一个线程,进行相应的网络操作,再使用handler异步操作主线程的UI更新。
代码:

public class BitmapimgActivity extends Activity {
    private ImageView imageView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bitmapimg);
        imageView = (ImageView) this.findViewById(R.id.imgs);

        new Thread() {
            public void run() {
                // 图片资源
                String url = "http://start.firefoxchina.cn/img/worldindex/logo.png";
                // 得到可用的图片
                Bitmap bitmap = getHttpBitmap(url);

                Message msg=new Message();
                msg.what=1;
                msg.obj=bitmap;
                handler.sendMessage(msg);
                // imageView.setBackgroundResource(R.drawable.ic_launcher);
            };

        }.start();

    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int s = msg.what;
            if(s==1){
                Bitmap bits=(Bitmap) msg.obj;

                // 显示
                imageView.setImageBitmap(bits);
            }

        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.bitmapimg, menu);
        return true;
    }

    /**
     * 获取网落图片资源
     * 
     * @param url
     * @return
     */
    public static Bitmap getHttpBitmap(String url) {
        URL myFileURL;
        Bitmap bitmap = null;
        try {
            myFileURL = new URL(url);
            // 获得连接
            HttpURLConnection conn = (HttpURLConnection) myFileURL
                    .openConnection();
            // 设置超时时间为6000毫秒,conn.setConnectionTiem(0);表示没有时间限制
            conn.setConnectTimeout(6000);
            // 连接设置获得数据流
            conn.setDoInput(true);
            // 不使用缓存
            conn.setUseCaches(false);
            // 这句可有可无,没有影响
            // conn.connect();
            // 得到数据流
            InputStream is = conn.getInputStream(); // 报错???????????????
            // 解析得到图片
            bitmap = BitmapFactory.decodeStream(is);
            // 关闭数据流
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return bitmap;

    }

}

2、本地图片(sdcard):

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView image1 = (ImageView) findViewById(R.id.iv1);  //获得ImageView对象
         /*为什么图片一定要转化为 Bitmap格式的!! */
        Bitmap bitmap = getLoacalBitmap("/sdcard/tubiao.jpg"); //从本地取图片(在cdcard中获取)  //
        image1 .setImageBitmap(bitmap); //设置Bitmap
      }



    /**
    * 加载本地图片
    * @param url
    * @return
    */
    public static Bitmap getLoacalBitmap(String url) {
         try {
              FileInputStream fis = new FileInputStream(url);
              return BitmapFactory.decodeStream(fis);  ///把流转化为Bitmap图片         

           } catch (FileNotFoundException e) {
              e.printStackTrace();
              return null;
         }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值