serenskye serenskye - 1 year ago 134
Android Question

Setting a bitmap using RemoteView not working

Im writing a widget and I need to download and set a bitmap on the layout. Everything I've tried doesn't seem to work.

I've created a test bitmap now to set on the view, [update] this works.

Bitmap.Config config = Bitmap.Config.ARGB_8888;
Bitmap bitmap = Bitmap.createBitmap(imageActiveWidth, imageHeight, config);
Canvas canvas = new Canvas(bitmap); // Load the Bitmap to the Canvas
Paint paint = new Paint();
canvas.drawRect(0, 0, imageActiveWidth, imageHeight, paint);
views.setImageViewBitmap(resId, bitmap);

using a resource file does work:

Bitmap placeholderBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.placeholder_medium);
views.setImageViewBitmap(imageSlotId, placeholderBitmap);

However using a downloaded bitmap does not seem work.
(after async task has downloaded bitmap, I have a method setBitmap which is one line:

views.setImageViewBitmap(resId, proxy);

Result - screen is just white, no bitmap

I'm really stumped on how to get this to work, because I need to be able to download bitmaps and set them.

Answer Source

Found a solution. I think its related to this bug:

Solved by changing by setBitmap method to the following:

private void setBitmap(RemoteViews views, int resId, Bitmap bitmap){
    Bitmap proxy = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(proxy);
    c.drawBitmap(bitmap, new Matrix(), null);
    views.setImageViewBitmap(resId, proxy);

And I needed to call: AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, views);
AFTER the bitmaps had been set.

For some reason this wasn't working when I came back to it. My view was an AdapterViewFlipper, so i used the above method with a call to widgetManager.notifyAppWidgetViewDataChanged(appWidgetId,; which caused the bitmaps to render.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download