android 线程池的应用

本文介绍了一个使用Java线程池加载并处理图片的例子。该示例通过创建固定大小的线程池来异步加载图片,并根据路径解码图片为Bitmap对象。如果指定的缩略图路径无效,则尝试从源路径加载图片。加载完成后,图片会通过回调函数展示到ImageView中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1     ExecutorService executorService = Executors

            .newFixedThreadPool(threadCount + 1);

           // 线程池的创建服务器

           Runnable calculateBitmapWorker = new Runnable() {
                @Override
                public void run() {
                    Bitmap thumb = null;
                    try {
                        if (isThumbPath) {
                            thumb = BitmapFactory.decodeFile(thumbPath);
                            if (null == thumb) {
                                thumb = revitionImageSize(sourcePath);
                            }
                        } else {
                            thumb = revitionImageSize(sourcePath);
                        }
                    } catch (Exception e) {


                    }
                    if (null == thumb) {
                        thumb = PickPhotoActivity.bimap;
                    }
                    put(path, thumb);
                    final Bitmap bmpToCallback = thumb;
                    if (null != callback) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                callback.imageLoad(iv, bmpToCallback, sourcePath);
                            }
                        });
                    }
                }
            };


            executorService.execute(calculateBitmapWorker);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值