android glide載入base64圖片
阿新 • • 發佈:2018-12-16
1,直接上程式碼
public static boolean isBase64Img(String imgurl){ if(!TextUtils.isEmpty(imgurl)&&(imgurl.startsWith("data:image/png;base64,") ||imgurl.startsWith("data:image/*;base64,")||imgurl.startsWith("data:image/jpg;base64,") )) { return true; } return false; }
public static void loadImageForTarget(SupportFragment fragment,String url,int error_resid,Target<Bitmap> target){ byte[] decode = null; if(isBase64Img(url)) { url = url.split(",")[1]; decode = Base64.decode(url, Base64.DEFAULT); } BitmapTypeRequest bitmapTypeRequest = Glide.with(fragment).load(decode==null ?url:decode).asBitmap(); if(error_resid !=0){ // load.error(error_resid); bitmapTypeRequest.placeholder(error_resid); }else{ bitmapTypeRequest.placeholder(DEFALT_ID); } bitmapTypeRequest.diskCacheStrategy(DiskCacheStrategy.RESULT); bitmapTypeRequest.dontAnimate(); bitmapTypeRequest.into(target); }
用glide下載來相容h5中的base64圖片