美文网首页
xUtils图片模块

xUtils图片模块

作者: Summer_27d1 | 来源:发表于2018-08-10 11:37 被阅读0次
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

相关文章

网友评论

      本文标题:xUtils图片模块

      本文链接:https://www.haomeiwen.com/subject/mqrkbftx.html