xUtils图片模块

image.png

image.png

image.png

image.png

image.png

使用:
一,下载图片

image.png

在服务器上下载图片:


image.png

使用loadDrawable()


image.png

使用loadFile()方法加载图片:
···

ImageOptions options = new ImageOptions.Builder()
// .setFadeIn(true)//设置加载图片的动画渐变效果
.setCircular(true)
.setAnimation(alphaAnimation)
.setFailureDrawableId(R.mipmap.ic_launcher)
.setLoadingDrawableId(R.mipmap.ic_launcher_round)
.setRadius(50)

                    .build();

···
···

  Callback.Cancelable cancelable3 = x.image().loadFile(
                    "http://pic2.sc.chinaz.com/files/pic/pic9/201808/zzpic13391.jpg",
                     options,
                    new Callback.CacheCallback<File>() {
                        @Override
                        public void onSuccess(File result) {
                            try {
                                //把文件类型的数据转换成字节数据
                                FileInputStream fi = new FileInputStream(result);
                                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                                byte buffer[] = new byte[512];
                                int length = -1;
                                while(  (length = fi.read(buffer)) != -1  ){
                                    bs.write(buffer,0,length);
                                    bs.flush();
                                }

                                File file1 = new File(Environment.getExternalStorageDirectory()
                                        +"/my1710.jpg");
                                FileOutputStream fs = new FileOutputStream(file1);
                                fs.write(bs.toByteArray(),0,bs.toByteArray().length);
                                fs.flush();

                                fi.close();
                                bs.close();
                                fs.close();

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            imageView.setImageBitmap(BitmapFactory.decodeFile(result.getAbsolutePath()));
                            Toast.makeText(MyActivity.this, "onSuccess", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {

                        }

                        @Override
                        public void onCancelled(CancelledException cex) {

                        }

                        @Override
                        public void onFinished() {

                        }

                        @Override
                        public boolean onCache(File result) {
                            Toast.makeText(MyActivity.this, "onCache", Toast.LENGTH_SHORT).show();
                            imageView.setImageBitmap(BitmapFactory.decodeFile(result.getAbsolutePath()));
                            return false;
                        }
                    }
            );

···


image.png

保存在sd卡

效果图:


image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容