okhttp图片加载

根据上篇完成依赖添加:点击打开链接


画主界面布局


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="com.example.yls.demoa.MainActivity">

    <Button
        android:id="@+id/btn_start"
        android:text="点击"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

初始化控件并设置点击事件

    private Button button;
    private ImageView imageView;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getLoadImg();
            }
        });
    }


完成编写getLoadImg()方法

 private void getLoadImg() {
        String url = "http://crp.gdcp.cn/data/uploadData/2018/440666666/0303//201711972628361.png";
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("Mainactivity", e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                InputStream inputStream = response.body().byteStream();
                //                在页面显示
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                Message message = new Message();
                message.what = GET_IMG;
                message.obj = bitmap;
                handler.sendMessage(message);

                //保存本地
//                String filePath = getFilesDir().getAbsolutePath()+ File.separator+"down.jpg";
//                FileOutputStream fileOutputStream = new FileOutputStream(filePath);
//                byte[] buffer = new byte[2048];
//                int len = 0;
//                while ((len = inputStream.read(buffer)) != -1){
//                    fileOutputStream.write(buffer,0,len);
//                }
//                fileOutputStream.flush();
//                Log.e("mainActivity","save end");
            }
        });
    }




改方法采取两种方式完成图片的加载以及下载功能,还应在handle中完成发消息机制,

将下载图片的信息发送到主线程中,以下为handle的初始化代码:


imageView = findViewById(R.id.image);
        button = findViewById(R.id.btn_start);
       
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
               
                if (message.what == GET_IMG){
                    Bitmap bitmap = (Bitmap) message.obj;
                    imageView.setVisibility(View.VISIBLE);
                    imageView.setImageBitmap(bitmap);
                }
                return false;
            }
        });



接受图片对象为Bitmap类,根据setVisibility()  和 setImageBitmap() 来设置图片的显示方式

最后在运行之前检查是否添加了网络访问权限,减少不必要的麻烦。




运行程序,查看结果


(图片为我学校官网上的图片,图片为我校老师,请勿随意转载) 





此次图片加载过程就完成,今日学习任务完成,整理一番重新温习一遍代码。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值