Android图片二次缓存案例

首先建立一个布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_height="match_parent"
    android:padding="15dp"
    tools:context=".Main3Activity">

    <ImageView
        android:id="@+id/iv_2"
        android:scaleType="fitCenter"
        android:layout_width="match_parent"
        android:layout_height="300dp" />
   <Button
       android:id="@+id/tu_1"
       android:layout_marginTop="500dp"
       android:layout_width="match_parent"
       android:text="Button"
       android:layout_height="wrap_content" />
</RelativeLayout>

点击按钮获取图片:

    @SuppressLint("AppCompatCustomView")
    public  class MyImageView extends ImageView {
        public static final int GET_DATA_SUCCESS = 1;
        public static final int NETWORK_ERROR = 2;
        public static final int SERVER_ERROR = 3;
        //子线程不能操作UI,通过Handler设置图片
        @SuppressLint("HandlerLeak")
        private Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case GET_DATA_SUCCESS:
                        Bitmap bitmap = (Bitmap) msg.obj;
                        setImageBitmap(bitmap);
                        break;
                    case NETWORK_ERROR:
                        Toast.makeText(getContext(), "网络连接失败", Toast.LENGTH_SHORT).show();
                        break;
                    case SERVER_ERROR:
                        Toast.makeText(getContext(), "服务器发生错误", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };

        public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }

        public MyImageView(Context context) {
            super(context);
        }

        public MyImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        //设置网络图片
        public void setImageURL(final String path) {
            //开启一个线程用于联网
            new Thread() {
                @Override
                public void run() {
                    try {
                        //把传过来的路径转成URL
                        URL url = new URL(path);
                        //获取连接
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        //使用GET方法访问网络
                        connection.setRequestMethod("GET");
                        connection.setConnectTimeout(10000);
                        //获取返回码
                        int code = connection.getResponseCode();
                        if (code == 200) {
                            InputStream inputStream = connection.getInputStream();
                            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                            //利用Message把图片发给Handler
                            Message msg = Message.obtain();
                            msg.obj = bitmap;
                            msg.what = GET_DATA_SUCCESS;
                            handler.sendMessage(msg);
                            inputStream.close();
                        } else {
                            handler.sendEmptyMessage(SERVER_ERROR);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        handler.sendEmptyMessage(NETWORK_ERROR);
                    }
                }
            }.start();
        }

    }



具体流程就是判断缓存中是否存在,若不存在开启一个线程,通过IO流将数据写入本地缓冲。在将信息传送到UI线程。
有几点注意:
1.UI线程不能进行网络操作
2.只有UI线程能操作ui界面

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值