Image Not loaded sometime and first time using Picasso in Android

public static void setBackgroundUrl(View view, String backgroundUrl, Transformation transformation,
Callback callback, String bgColorCode, boolean toggleImageVisibility) {
if (UiUtils.INSTANCE.isColorCodeValidHex(bgColorCode)) {
view.setBackground(ResourceProvider.Companion.getInstance().getColorDrawable(bgColorCode));
return;
}
if (StringUtil.isNullOrEmpty(backgroundUrl)) {
return;
}
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {

if (view != null) {
if(view.getWidth() > 0 && view.getHeight() > 0) {
bitmap = Bitmap.createScaledBitmap(bitmap, view.getWidth(), view.getHeight(), true);
}
view.setBackground(new BitmapDrawable(resourceProvider.getResources(), bitmap));
view.setTag(null);
if (toggleImageVisibility) {
view.setVisibility(View.VISIBLE);
}
}
if (callback != null) {
callback.onSuccess();
}
}


@Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
if (callback != null) {
callback.onError(e);
}
if (view != null && toggleImageVisibility) {
view.setVisibility(View.GONE);
}
}

@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
RequestCreator request = Picasso.get()
.load(backgroundUrl)
.config(Bitmap.Config.RGB_565);

if (transformation != null) {
request.transform(transformation);
}
request.into(target);
view.setTag(target);
}
Action(Picasso picasso, T target, Request request, int memoryPolicy, int networkPolicy,
int errorResId, Drawable errorDrawable, String key, Object tag, boolean noFade) {
this.picasso = picasso;
this.request = request;
this.target =
target == null ? null : new RequestWeakReference<>(this, target, picasso.referenceQueue);

this.memoryPolicy = memoryPolicy;
this.networkPolicy = networkPolicy;
this.noFade = noFade;
this.errorResId = errorResId;
this.errorDrawable = errorDrawable;
this.key = key;
this.tag = (tag != null ? tag : this);
}
abstract class ImageProgressCallback {
var callbackHolder: Any? =
null // required to avoid garbage collection in Image Lib (Picasso for weak ref object)
abstract fun onBitmapLoaded(bitmap: Bitmap?, from: ImageLoadFrom?)
abstract fun onBitmapFailed(e: java.lang.Exception?, errorDrawable: Drawable?)
abstract fun onPrepareLoad(placeHolderDrawable: Drawable?)
}
override fun loadImage(imageView: ImageView?, progressCallback: ImageProgressCallback?, statusCallback: ImageStatusCallback?, imageRequest: ImageRequest) {
--- some code
when {

progressCallback != null -> {
val target = ImgTarget(progressCallback)
progressCallback.callbackHolder = target
PicassoUtil.cancelRequest(target)
request.into(target)
return
}

imageView != null -> {
cancelRequest(imageView)
request.into(imageView)
return
}
}

--

--

--

Principal Software Engineer , Mobile Engineering

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

8 Uses of SCP Commands in Linux

A big blueberry

Deploy your WebApp to Azure App Service through Local Git.

HELLO WORLD!

ConcurrentHashMap in Objective C

CS371p Fall 2021: Sean Yu: Final Entry

pentaho 8.2, mac, java, installation, blank screen, SWT

Array: A Well Known Data Structure

A hobby coder and a gamer — I am usually very confused!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Amit Gupta

Amit Gupta

Principal Software Engineer , Mobile Engineering

More from Medium

Setup New Unity Input System

const [DE, STRUCTUR, ING] in JS

How to complete all tasks in the OmniFlix Studio | FlixNet-3 Campaign

What means of IOT Devices & Mining ?