存储图片到本地、提取本地存储的图片

本文介绍了一种实现头像图片缓存的方法,包括如何将远程获取的头像保存到本地并进行覆盖更新,以及如何从本地读取已缓存的头像图片并显示在界面上。
/**************保存头像到本地**************************/
final String fileName = mobile + ".jpg";
File file = new File(Constants.ICON + "/" + fileName);
if (file.exists()) {// 先判断本地是否有缓存,有就删了
// 先删除
file.delete();
}
// 再保存新的到本地
ThreadPoolManager.getInstance().addTask(new Runnable() {
@Override
public void run() {
Bitmap bp = ImageUtil.getBitmaoByurl(headPath);
if (bp != null) {
FileUtil.saveFile(context, fileName, bp);
}
}

});



/***********根据文件名提取本地的头像图片*******************************/
File file2 = new File(Constants.ICON+"/"+mobile2+".jpg");
if(file2.exists()){
try {
Uri uri = Uri.fromFile(file2);
Bitmap bitmap = BitmapFactory.decodeStream(FriendActivity.this.getContentResolver().openInputStream(uri));
Bitmap roundCorner = LoadingImage.toRoundCorner(bitmap, 360);
iv_head.setImageBitmap(roundCorner);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值