build.gradle
implementation "com.github.bumptech.glide:glide:4.9.0"
代碼部分:
public void fetchImage(View view) {
final ImageView carView = findViewById(R.id.iv);
final String temp = getExternalCacheDir().getAbsolutePath() + "/abc.jpg";
String url = "";
url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1572796331763&di=678053d81344db34636ce5f62ae320d1&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01f3cf5618cbd36ac7255b14dfafd4.jpg%401280w_1l_2o_100sh.jpg";
Glide.with(this)
.asFile()
.load(url)
.placeholder(android.R.drawable.ic_delete)
.thumbnail(0.1f)//此縮略圖機(jī)制無(wú)法完成對(duì)下載圖片的壓縮
.into(new CustomTarget<File>() {
@Override
public void onResourceReady(@NonNull File resource, @Nullable Transition<? super File> transition) {
//實(shí)現(xiàn)壓縮,并重新生成BitMap對(duì)象
Bitmap bitmap = BitmapFactory.decodeFile(resource.getAbsolutePath(), null);
try {
FileOutputStream out = new FileOutputStream(temp);
bitmap.compress(Bitmap.CompressFormat.JPEG, 4, out);
bitmap = BitmapFactory.decodeFile(temp);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
carView.setImageBitmap(bitmap);
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
}
最后編輯于 :
?著作權(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ù)。