views:

89

answers:

1

Hi Guys,

I am trying to retrieve an BLOB image data from MySQL database using PHP and use that data to convert it to a bitmap and display it on my AppWidget. What happens is the widget initial layout remains on screen and never gets updated. Here are the snippets of my code.

Widgetinfo xml

<appwidget-provider
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:minWidth="294dp"
 android:minHeight="72dp"
 android:updatePeriodMillis="60000"
 android:initialLayout="@layout/tuwidget"></appwidget-provider>

Widget Layout xml(I have used the 4x1 .psd example layout from Google as my initial layout)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tuwidget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/tuwidget_img_btn"
android:src="@drawable/widgetinitial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</ImageView>
</LinearLayout>

Here is how I create my bitmap object from BLOB

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/RetrieveImage.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
  byte[] buf = new byte[1024];
  int n = 0;
  while ((n=is.read(buf))>=0)
  {
     baos.write(buf, 0, n);
  }
byte[] bytes = baos.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

This is where I start updating the Remoteviews for the AppWidget Update.

RemoteViews updateViews = null;
updateViews = new RemoteViews(context.getPackageName(), R.layout.tuwidget);
updateViews.setImageViewBitmap(R.id.tuwidget_img_btn, bitmap);
is.close();
return updateViews;

Basically thats the code. While debugging I found that bitmap is definitely not null so I dont understand why the ImageView on the AppWidget is not updated. Just to be sure I will also add the basic construct of my Widget Class extending the AppWidgetProvider.

public class TooyoouWidget extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
                context.startService(new Intent(context, UpdateService.class));
    }

    public static class UpdateService extends Service {
        @Override
        public void onStart(Intent intent, int startId) {
                    RemoteViews updateViews = buildUpdate(this);
                    ComponentName thisWidget = new ComponentName(this, TooyoouWidget.class);
                    AppWidgetManager manager = AppWidgetManager.getInstance(this);
                    manager.updateAppWidget(thisWidget, updateViews);
        }  

Rest of the stuff happens here.

public RemoteViews buildUpdate(Context context) {
.....
return updateViews;
}

One more thing to underscore - I have added the receiver tag in my AndroidManifest.xml

<receiver
   android:name=".TooyoouWidget">
   <intent-filter>
   <action
    android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
   </intent-filter>
   <meta-data
    android:name="android.appwidget.provider"
    android:resource="@xml/tuwidget_info"/>
</receiver>

I am sorry for being very descriptive but I am learning and I want to give out all information so that pros like you can analyze and help. Any feedback is highly appreciated.Until then I will try GOOGLE again.

Cheers.

+1  A: 

YES YES YES....found the mistake. Above code works all the way. I was missing an important part in AndroidManifest.xml

Aakash
<service android:name=".TooyoouWidget$UpdateService" />
Aakash