Android 下载、显示图片

复制代码
一、新建PictureLoader 

public class PictureLoader {
    private ImageView loadImg;
    private String imgUrl;
    private byte[] picByte;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x123) {
                if (picByte != null) {
                    Bitmap bitmap = BitmapFactory.decodeByteArray(picByte, 0, picByte.length);
                    loadImg.setImageBitmap(bitmap);
                }
            }
        }
    };

    public void load(ImageView loadImg, String imgUrl) {
        this.loadImg = loadImg;
        this.imgUrl = imgUrl;
        Drawable drawable = loadImg.getDrawable();
        if(drawable != null && drawable instanceof BitmapDrawable) {
            Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
            if(bitmap != null && !bitmap.isRecycled()) {
                bitmap.recycle();
            }
        }
        new Thread(runnable).start();
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            try {
                URL url = new URL(imgUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setReadTimeout(10000);
                if (conn.getResponseCode() == 200) {
                    InputStream in = conn.getInputStream();
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    byte[] bytes = new byte[1024];
                    int length = -1;
                    while ((length = in.read(bytes)) != -1) {
                        out.write(bytes, 0, length);
                    }
                    picByte = out.toByteArray();
                    in.close();
                    out.close();
                    handler.sendEmptyMessage(0x123);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };

}

二、新建Activity界面

复制代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button showBtn;
    private ImageView showImg;
    private ArrayList<String> urls;
    private  int curPos=0;
    private PictureLoader loader;


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

        showBtn=(Button)findViewById(R.id.showBtn);
        showImg=(ImageView)findViewById(R.id.img_show);
       loader=new PictureLoader();

        showBtn.setOnClickListener(this);

        urls=new ArrayList<>();
        urls.add("https://img1.baidu.com/it/u=2613963793,934854936&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800");
        urls.add("https://img2.baidu.com/it/u=2453741982,2770329727&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800");
        urls.add("https://img0.baidu.com/it/u=494561658,4056512075&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500");
        urls.add("https://img2.baidu.com/it/u=2389087729,70628867&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=750");
        urls.add("https://k.sinaimg.cn/n/sinakd10100/50/w660h990/20240323/d50c-5faa98beb0d96d35c6ae9444d98b3c0d.jpg/w700d1q75cms.jpg");
        urls.add("https://img0.baidu.com/it/u=2673688411,90488494&fm=253&fmt=auto&app=138&f=JPEG?w=480&h=800");
        urls.add(" https://img1.baidu.com/it/u=1682400140,1262357671&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500");
        urls.add("https://img1.baidu.com/it/u=2064828371,3291434219&fm=253&fmt=auto&app=138&f=JPEG?w=360&h=360");
        urls.add("https://img2.baidu.com/it/u=2148980033,2323997411&fm=253&fmt=auto&app=138&f=JPEG?w=759&h=500");

    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.showBtn:
                if(curPos>9){
                    curPos=0;
                }
                Logger.d(urls.get(curPos));
                loader.load(showImg,urls.get(curPos));

                curPos++;
                break;
        }

    }
}
相关推荐
Jouzzy7 小时前
【Android安全】Ubuntu 16.04安装GDB和GEF
android·ubuntu·gdb
极客先躯7 小时前
java和kotlin 可以同时运行吗
android·java·开发语言·kotlin·同时运行
Good_tea_h10 小时前
Android中的单例模式
android·单例模式
计算机源码社14 小时前
分享一个基于微信小程序的居家养老服务小程序 养老服务预约安卓app uniapp(源码、调试、LW、开题、PPT)
android·微信小程序·uni-app·毕业设计项目·毕业设计源码·计算机课程设计·计算机毕业设计开题
丶白泽15 小时前
重修设计模式-结构型-门面模式
android
晨春计16 小时前
【git】
android·linux·git
标标大人17 小时前
c语言中的局部跳转以及全局跳转
android·c语言·开发语言
竹林海中敲代码18 小时前
Qt安卓开发连接手机调试(红米K60为例)
android·qt·智能手机
木鬼与槐18 小时前
MySQL高阶1783-大满贯数量
android·数据库·mysql
iofomo18 小时前
【Abyss】Android 平台应用级系统调用拦截框架
android·开发工具·移动端