android 与Bitmap、runnable、handle,实现网络图,设置为imageview的背景图

简述,不得不说和这几个大神交涉,那简直就是勾心斗角,各种调试,还不是因为了解得太少了。不过不得不说,这个bitmap简直就是强大,runnable、handle也很实用,要不然,也不花这功夫了。好,入题;

1、需求:从网络请求图片成功后,将其设置为imageview的背景图;

2、代码:


//调用时,如此写:
new Thread(runnable).start();


   private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Log.e(TAG, "handleMessage: "+msg.what );
            switch (msg.what){
                
                case CaptureCamera:
               		imgtest.setImageBitmap(bmp_title);
                    getCaptureCemera();
                    tv_test.setText("设置成功");
                    break;


        private Runnable runnable=new Runnable() {
        @Override
        public void run() {
            while (photoUrl=="") {
                try {
                    photoUrl = MyApplaction.getOpenSDK().captureCamera("736175838", 1);//必须在子线程中获取
                } catch (BaseException e) {
                    e.printStackTrace();
                }
                Log.e(TAG, "new thread,photoUrl: " + photoUrl);
            }
            if (photoUrl!=""){
                final byte[] data=getImages(photoUrl);//子线程中执行
                bmp_title=BitmapFactory.decodeByteArray(data,0,data.length);//子线程中执行
                mHandler.sendEmptyMessage(CaptureCamera);//主线程中更新ui
            }
        }
    };

    //加载图片
    private byte[] getImages(String path) {
        try {
            URL url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setReadTimeout(6 * 1000);
            InputStream inputStream = null;
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = -1;
            if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                inputStream = httpURLConnection.getInputStream();
                while ((len = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, len);
                }
                outputStream.close();
                inputStream.close();
            }
            return outputStream.toByteArray();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


外接sdk获取图片的地址必须在子线程中运行,所以在写在runnable中,并且在调用的时候如此写:new Thread(runnable).start();,这样才可以;文章中哪里该写在哪里的要求已经注明。
更多bitmap方法参考https://blog.csdn.net/zjws23786/article/details/79892150;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jian11058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值