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

    }
}
相关推荐
周全全7 分钟前
MySQL报错解决:The user specified as a definer (‘root‘@‘%‘) does not exist
android·数据库·mysql
- 羊羊不超越 -42 分钟前
App渠道来源追踪方案全面分析(iOS/Android/鸿蒙)
android·ios·harmonyos
wk灬丨1 小时前
Android Kotlin Flow 冷流 热流
android·kotlin·flow
千雅爸爸1 小时前
Android MVVM demo(使用DataBinding,LiveData,Fresco,RecyclerView,Room,ViewModel 完成)
android
晨曦_子画2 小时前
编程语言之战:AI 之后的 Kotlin 与 Java
android·java·开发语言·人工智能·kotlin
孤客网络科技工作室2 小时前
AJAX 全面教程:从基础到高级
android·ajax·okhttp
Mr Lee_4 小时前
android 配置鼠标右键快捷对apk进行反编译
android
顾北川_野4 小时前
Android CALL关于电话音频和紧急电话设置和获取
android·音视频
&岁月不待人&4 小时前
Kotlin by lazy和lateinit的使用及区别
android·开发语言·kotlin