Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setImageViewBitmap not working in android widget

When I try to put a bitmap on a widget I used this :

theBitmap = Bitmap.createBitmap(WW, HH,
            Bitmap.Config.ARGB_8888);
.
.
// draw something using a canvas
.
.
rviews.setImageViewBitmap(R.id.time,theBitmap);

This works on my phone but not on my Galaxy tablet or Galaxy Note,

if I copy theBitmap into a new ARGB_4444 then it works on all devices:

Bitmap clone= theBitmap.copy(Bitmap.Config.ARGB_4444, false); // workaround
rviews.setImageViewBitmap(R.id.time, clone);
like image 632
AVEbrahimi Avatar asked Apr 15 '12 08:04

AVEbrahimi


1 Answers

First, without your workaround, check your logcat output for:

ERROR/JavaBinder(20204): !!! FAILED BINDER TRANSACTION !!!

You're probably hitting the file size limit for the IPC transaction that holds your changes to the RemoteViews hierarchy.

When you copy the bitmap with the Bitmap.Config.ARGB_4444 config, each pixel will be represented with 2 bytes, whereas Bitmap.Config.ARGB_8888 (the default config on 2.3 and up) each pixel is 4 bytes [Source].

By using your workaround, you're passing half as much data, and thus, squeaking by the size limit. This post suggests the size limit is 1MB, but I'm not sure if that varies per OS version or manufacturer.

Update: A different approach to passing an image would be to not send the bitmap itself, but rather pass a URI to the file on disk. Then, you need to make sure that other processes will have read access to your file (the launcher will run in a different process for example). If you're writing out files into your /data/data/my.package.name/ directory, you can do this by specifying MODE_WORLD_READABLE on Context.openFileOutput(). For example:

remoteViews.setUri(R.id.time, "setImageURI", Uri.fromFile(file));
like image 95
wsanville Avatar answered Oct 10 '22 07:10

wsanville