![](https://img.haomeiwen.com/i12083643/261a14dc6709d321.png)
![](https://img.haomeiwen.com/i12083643/39a586dab56258a3.png)
![](https://img.haomeiwen.com/i12083643/135a536b8ba31fd0.png)
![](https://img.haomeiwen.com/i12083643/58e7264b9ca9b905.png)
![](https://img.haomeiwen.com/i12083643/1222f78513e4e109.png)
使用:
一,下载图片
![](https://img.haomeiwen.com/i12083643/10ffb28c7e6c8765.png)
在服务器上下载图片:
![](https://img.haomeiwen.com/i12083643/82217817bd72b781.png)
使用loadDrawable()
![](https://img.haomeiwen.com/i12083643/a473b6235673ed91.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;
}
}
);
···
![](https://img.haomeiwen.com/i12083643/f948fc3db6f026ee.png)
保存在sd卡
效果图:
![](https://img.haomeiwen.com/i12083643/534b9dbb29730688.png)
网友评论