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

    }
}
相关推荐
恋猫de小郭11 小时前
Android Studio 正式版 10 周年回顾,承载 Androider 的峥嵘十年
android·ide·android studio
aaaweiaaaaaa14 小时前
php的使用及 phpstorm环境部署
android·web安全·网络安全·php·storm
工程师老罗16 小时前
Android记事本App设计开发项目实战教程2025最新版Android Studio
android
pengyu19 小时前
系统化掌握 Dart 编程之异常处理(二):从防御到艺术的进阶之路
android·flutter·dart
消失的旧时光-194320 小时前
android Camera 的进化
android
基哥的奋斗历程21 小时前
Openfga 授权模型搭建
android·adb
Pakho love1 天前
Linux:文件与fd(被打开的文件)
android·linux·c语言·c++
勿忘初心912 天前
Android车机DIY开发之软件篇(九) NXP AutomotiveOS编译
android·arm开发·经验分享·嵌入式硬件·mcu
lingllllove2 天前
PHP中配置 variables_order详解
android·开发语言·php
消失的旧时光-19432 天前
Android-音频采集
android·音视频