I want to load an image from url in onDraw
method for the view preview. I tried to do this using Picasso. But, I get an error in the xml. This is the onDraw
method.
@Override
protected void onDraw(Canvas canvas) {
this.canvas = canvas;
Picasso.get()
.load(currentUrl)
.into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Drawable d = new BitmapDrawable(getResources(), bitmap);
d.draw(canvas);
}
@Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawText("Error to load",getWidth() / 2, getHeight() / 2,paint);
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
}
But, Picasso complains about no context to load the file from. This is the error:
And the text:
java.lang.IllegalStateException: context == null
at com.squareup.picasso.Picasso.get(Picasso.java:681)
at com.sambhav2358.badgegeneratorlibrary.BadgeView.setLogo(BadgeView.java:189)
at com.sambhav2358.badgegeneratorlibrary.BadgeView.init(BadgeView.java:69)
at com.sambhav2358.badgegeneratorlibrary.BadgeView.<init>(BadgeView.java:91)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:352)
at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:189)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:149)
at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:301)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:417)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:428)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:332)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1127)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1101)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
at android.view.LayoutInflater.inflate(LayoutInflater.java:686)
at android.view.LayoutInflater.inflate(LayoutInflater.java:505)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:361)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:436)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:121)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:736)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$7(RenderTask.java:892)
at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout$2.run(RenderExecutor.kt:187)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
CodePudding user response:
AFAIK, it is not possible to obtain Context
reference if the application is not running.
CodePudding user response:
Did you try to use Glide? It is another library for charge images.
repositories {
mavenCentral()
}
dependencies {
compile 'com.github.bumptech.glide:glide:3.6.1'
compile 'com.android.support:support-v4:23.1.1'
}
Try to do this
private void initLocal() {
// Charge img
Glide.with(this).load(URL).into(imgView);
}