-2

I have a PictureHDFragment in which i use the method Html.fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler); i would like the method getDrawable (which is called automatically by Html.fromHtml when it encounters the tag img) to start a thread in order to download the image out of the Fragment and avoid blocking of all application.

Then i have created an external class HttpImageGetter that implements ImageGetter and extend IntentService.

public class HttpImageGetter extends IntentService implements Html.ImageGetter {

private Bitmap bitmap = null;
private Context context;

public HttpImageGetter(Context context) {
    super("get HTML Image");
    this.context = context;
}

@Override
public Drawable getDrawable(String source) {
    //mvc.controller.getDrawable(getContext(), source);

    //Context context = getApplicationContext();
    startService(context, source);
    BitmapDrawable bitmapDrawable;

    if (bitmap == null)
        bitmapDrawable = null;
    else {
        //bitmapDrawable = bitmapDrawableIterator.next();
        bitmapDrawable = new BitmapDrawable(bitmap);
        bitmapDrawable.setBounds(0, 0, bitmapDrawable.getIntrinsicWidth(), bitmapDrawable.getIntrinsicHeight());
    }
    //BitmapDrawable bitmapDrawableExample = new BitmapDrawable(mvc.model.getHDImage());
    //bitmapDrawableExample.setBounds(0, 0, bitmapDrawableExample.getIntrinsicWidth(), bitmapDrawableExample.getIntrinsicHeight());

    return bitmapDrawable/*bitmapDrawableExample*/;
}

private final static String ACTION = "getImage";

private void startService(Context context, String source){
    Intent intent = new Intent(context/*getApplicationContext()*/, HttpImageGetter.class);
    intent.setAction(ACTION);
    intent.putExtra("source", source);
    context.startService(intent);
}

@Override
protected void onHandleIntent(@Nullable Intent intent) {
    String source = intent.getStringExtra("source");
    //Bitmap dr = null;

    try {
        URL url = new URL(source);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
        //dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

But this not working and launches RuntimeException, in particular:

java.lang.RuntimeException: Unable to instantiate service com.android.mattia.flickclient.Controller.HttpImageGetter: java.lang.InstantiationException: java.lang.Class<com.android.mattia.flickclient.Controller.HttpImageGetter> has no zero argument constructor
                                                                                    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3446)
                                                                                    at android.app.ActivityThread.-wrap6(ActivityThread.java)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1725)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:154)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6692)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
                                                                                 Caused by: java.lang.InstantiationException: java.lang.Class<com.android.mattia.flickclient.Controller.HttpImageGetter> has no zero argument constructor
                                                                                    at java.lang.Class.newInstance(Native Method)
                                                                                    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3443)
                                                                                    at android.app.ActivityThread.-wrap6(ActivityThread.java) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1725) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                    at android.os.Looper.loop(Looper.java:154) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6692) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 

So I understand that the problem is the parameter passed to the builder, then I have tryed to removed it and get the context directly in the HttpImageGetter class by the method getApplicationContext but this throw NullPointerException.

How can I resolve this problem?

Thanks

matz_
  • 11
  • 3

1 Answers1

0

Try to add this instead existing constructor: public HttpImageGetter() {super("get HTML Image");} If you need context in service, just use service as a context.

Hetfieldan24
  • 198
  • 11