Android将Bitmap保存至本地缓存,网络图片转Bitmap
程序员文章站
2022-06-21 19:18:23
使用Glide工具,在build.gradle添加Glide,大括号里面不用写,我这里写是因为和我的其它第三方有冲突implementation("com.github.bumptech.glide:glide:4.11.0") { exclude group: "com.android.support"}annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'获取BitmapGlide.with(get....
使用Glide工具,在build.gradle添加Glide,大括号里面不用写,我这里写是因为和我的其它第三方有冲突
implementation("com.github.bumptech.glide:glide:4.11.0") { exclude group: "com.android.support" }
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
获取Bitmap
Glide.with(getActivity()) .asBitmap() .load("网络图片地址") .into(new SimpleTarget<Bitmap>() { @Override public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { uri = bitmap2uri(getActivity(), resource); } });
传入上下文参数和Bitmap,保存至本地缓存目录,返回Uri对象
private Uri bitmap2uri(Context c, Bitmap b) {//c.getCacheDir() // /Android/data/你的报名/cache/1600739295328.jpg File path = new File(c.getExternalCacheDir() + File.separator + System.currentTimeMillis() + ".jpg"); L.e("getAbsolutePath==="+path.getAbsolutePath()+" ===getAbsolutePath==="+path.getParent()); try { OutputStream os = new FileOutputStream(path); b.compress(Bitmap.CompressFormat.JPEG, 100, os); os.close(); return Uri.fromFile(path); } catch (Exception ignored) { } return null; }
Uri转Bitmap,并复制bitmap
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)); bmcopy = bitmap.copy(Bitmap.Config.ARGB_8888, true);
本文地址:https://blog.csdn.net/qq734048504/article/details/108726470