xUtils图片模块
2018-08-10 本文已影响0人
Summer_27d1





使用:
一,下载图片

在服务器上下载图片:

使用loadDrawable()

使用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;
}
}
);
···

保存在sd卡
效果图:
