Background.exit() parameter cannot be bitmap or bitmap buffer

I wanted to download a picture using makeImageRequest in the background service, but It seems that Background.exit() cannot return a bitmap

  • Correct.  See the documentation on Background.exit():

    Data must be one of the following types:

    If a bitmap was allowed, you're also limited to 8k with Background.exit(), so that's something else to remember if this changes

  • Thanks for your reply, Will this change in the next few versions, I wish I could download weather icons in background service, otherwise, I have to packet icons of all weathers.

  • I'd go with just building the icons into your app vs downloading them.  That way you know you have the icon instead of trying to download them in the background.  Let the background just deal with getting the data itself.

  • I would also really like a way to return a Bitmap via Background.exit() or to instantiate a Bitmap class with a Base64 encoded string.

    Use Case:  I want the user to be able to display one bitmap from a set of 1,000 bitmaps.  It isn't feasible to include all 1,000 bitmaps with the watchface because of size limitations, even though each Bitmap is quite small.

    Allowing a watch face to make a call to a Web API via a background event and store the returned Bitmap is not possible based on the return type limitations for Background.exit().

    Another solution would be to include a constructor to Toybox::WatchUI::Bitmap that can take in a Base64 encoded string in order to create a BitmapResource instance (which could be retrieved via Background.exit()).

  • I have a solution

    first, convert an image to an array

    im=im.convert("L").resize((width,height))

    X=0
    Y=0
    arr=[]
    arr_num=0
    for i in im.getdata():


        this_point=i/85

        arr_num+=this_point*4**X

        X=X+1
        if X==im.size[0] :
            arr.append(arr_num)
            arr_num=0
            Y=Y+1
            X=0

    print arr

    //in the watchfaceview, draw the picture using the array.

    hidden function draw_arr(dc,x_start,y_start,arr,color)
    {

    x_start=x_start.toNumber();
    y_start=y_start.toNumber();

    var colorarr=color_arr(color,bg_color);

    var arr_content;
    var color_index;
    var color;
    var y;
    var x;
    var width=0;
    var width_temp;
    for (var i=0;i<arr.size();i++)
    {
    arr_content=arr[i];
    y=y_start+i;
    x=x_start;
    width_temp=0;
    while (arr_content)
    {
    color_index=arr_content&&0x3;
    color=colorarr[color_index.toNumber()];
    dc.setColor(color,Graphics.COLOR_TRANSPARENT);
    dc.drawPoint(x, y);

    arr_content=arr_content>>2;
    x++;
    }
    width_temp=x-x_start;
    if(width_temp>width)
    {
    width=width_temp;
    }
    }
    //System.println("width="+width);
    return width;
    }

    hidden function color_arr(foregroundcolor,backgroundcolor)
    {
    var arr=new[4];
    var fr,fg,fb;
    var br,bg,bb;
    fb=foregroundcolor%0x100;
    foregroundcolor=foregroundcolor>>8;
    fg=foregroundcolor%0x100;
    foregroundcolor=foregroundcolor>>8;
    fr=foregroundcolor%0x100;

    bb=backgroundcolor%0x100;
    backgroundcolor=backgroundcolor>>8;
    bg=backgroundcolor%0x100;
    backgroundcolor=backgroundcolor>>8;
    br=backgroundcolor%0x100;

    var r,g,b,color;
    for (var i=0;i<4;i++)
    {
    r=((fr)*i+(br)*(3-i))/3;
    g=((fg)*i+(bg)*(3-i))/3;
    b=((fb)*i+(bb)*(3-i))/3;
    color=r*0x10000+g*0x100+b;
    arr[i]=color;
    }
    return arr;
    }

    the problem is that, the function "draw_arr" executed too long, that you should always draw the array to the bitmapbuffer first , no need draw every time using it.