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;
        }

    }
}
相关推荐
li_liuliu11 分钟前
Android4.4 在系统中添加自己的System Service
android
C4rpeDime2 小时前
自建MD5解密平台-续
android
鲤籽鲲4 小时前
C# Random 随机数 全面解析
android·java·c#
m0_548514778 小时前
2024.12.10——攻防世界Web_php_include
android·前端·php
凤邪摩羯8 小时前
Android-性能优化-03-启动优化-启动耗时
android
凤邪摩羯8 小时前
Android-性能优化-02-内存优化-LeakCanary原理解析
android
喀什酱豆腐9 小时前
Handle
android
m0_7482329210 小时前
Android Https和WebView
android·网络协议·https
m0_7482517210 小时前
Android webview 打开本地H5项目(Cocos游戏以及Unity游戏)
android·游戏·unity
m0_7482546612 小时前
go官方日志库带色彩格式化
android·开发语言·golang