-3

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: enter image description here

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)

My entire class link is here

Sambhav Khandelwal
  • 3,585
  • 2
  • 7
  • 38
  • Does this answer your question? [How to use isInEditMode() to see layout with custom View in the editor](https://stackoverflow.com/questions/15423149/how-to-use-isineditmode-to-see-layout-with-custom-view-in-the-editor) – Primož Ivančič Apr 06 '22 at 10:07
  • uhhhmmm. No. But, how can I get the context even when using edit mode – Sambhav Khandelwal Apr 06 '22 at 10:09
  • Don't do something like this in `onDraw` .. have a `Bitmap` in your Custom view and Load The Image in UI Component like in `Activity` then set the Bitmap To the View and Invalidate the View .. This will also help u to handle error case like if image loading failed some something else .. – ADM Apr 06 '22 at 11:57
  • can you share the code? I did not get u exactly – Sambhav Khandelwal Apr 06 '22 at 15:11
  • 1
    Note the stack trace: something is wrong with your `BadgeView.setLogo` method - we'd need to see that to know what's wrong. – Ryan M Apr 16 '22 at 11:15
  • @RyanM Sure. I can post that. But, it is just nothing. Only loading a url with picasso. U still want me to? I can give my class link on github – Sambhav Khandelwal Apr 16 '22 at 11:22
  • simply use imageView.context – Atif AbbAsi Apr 16 '22 at 11:35
  • @AtifAbbAsi But, how can I pass context to picasso? It does not need it! – Sambhav Khandelwal Apr 17 '22 at 04:10
  • you need to initialize your Picasso in the application class then. or check if your app is in the background or not maybe that's why its getting context issue. – Atif AbbAsi Apr 18 '22 at 05:53
  • @AtifAbbAsi . It works on real and emualtor devices. But, want to use picasso as the view's preview. My question is very clear i guess. – Sambhav Khandelwal Apr 18 '22 at 05:56

2 Answers2

0

AFAIK, it is not possible to obtain Context reference if the application is not running.

Primož Ivančič
  • 1,984
  • 1
  • 17
  • 29
0

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);
    }