xUtils圖片模塊

image.png

image.png

image.png

image.png

image.png

使用:
一,下載圖片

image.png

在服務(wù)器上下載圖片:


image.png

使用loadDrawable()


image.png

使用loadFile()方法加載圖片:
···

ImageOptions options = new ImageOptions.Builder()
// .setFadeIn(true)//設(shè)置加載圖片的動(dòng)畫漸變效果
.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 {
                                //把文件類型的數(shù)據(jù)轉(zhuǎn)換成字節(jié)數(shù)據(jù)
                                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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容