The following 3 methods (from RemoteViews
class) can be used to update an image on a Home Screen widget, on Android:
setImageViewResource (int viewId, int srcId)
setImageViewUri (int viewId, Uri uri)
setImageViewBitmap (int viewId, Bitmap bitmap)
But after lot of research, I found that setImageViewUri()
is the best one to use, to avoid the error "**!!! FAILED BINDER TRANSACTION !!!**"
that can happen when updating the widget. But why is this the case? I couldn't find any proper reason or JavaDoc documentation.
It's possible if you REALLY want to do it, but it costs a lot of memory and is also slow. It might not work if you have an older phone and a big bitmap. You could just pass it as an extra, for example
intent.putExtra("data", bitmap)
. ABitmap implements Parcelable
, so you can put it in an extra and set it usingsetImageViewBitmap (int viewId, Bitmap bitmap)
.If you want to pass it in between activities, I would store it in a file. That's more efficient, and less work for you. You can create private files in your data folder using
MODE_PRIVATE
that are not accessible to any other app.If you pass it as a Parcelable, you're bound to get a JAVA BINDER FAILURE error. So, the solution is this: If the bitmap is small, like, say, a thumbnail, pass it as a byte array and build the bitmap for display in the next activity. For instance:
in your calling activity...
...and in your receiving activity
Some people recommend using setImageViewUri() to reduce the risk of freezing the widgets on the Home Screen. See https://code.google.com/p/android/issues/detail?id=28216#c102