Reputation: 12989
I'm trying to fetch an image from a remote URL and put it into an ImageView. The ImageView itself is within an app widget, so from my current understanding that means it is encapsulated within a RemoteViews and isn't really handled directly.
But I can never get the app widget to display an image. I'm fetching the image as a Bitmap from a remote URL using an AsyncTask extension, and it seems like the image is being fetched OK (a non-null bitmap is returned anyway), though as yet I can't display it to confirm.
I reckon I must be using the wrong method to actually update the app widget's ImageView.
Here is all the relevant code:
layout/new_app_widget.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_margin" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/content" />
</RelativeLayout>
relevant bit from NewAppWidget.java:
public class NewAppWidget extends AppWidgetProvider {
...
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
String strURL = "http://example.com/YqLOSfr4.png";
new ImageFetchAsyncTask(strURL, R.id.imageView).execute();
// ^^^^^
// in the above, I pass the URL and the id of the imageview, fetch
// the bitmap, and in the onPostExecute() method I use
// setImageViewBitmap(viewId, bitmap) to load the bitmap into the imageview
//
// I don't have the code to hand, but I can provide it. For now, someone
// may be able to tell me somewhere else I'm doing something wrong
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mike.widgetapptest" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:debuggable="true"
<activity
android:name=".MyActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".NewAppWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/new_app_widget_info" />
</receiver>
</application>
</manifest>
Upvotes: 1
Views: 1556
Reputation: 12989
Classic beginner's error... I was attempting to do something immediately following an async task, expecting the async task to have completed. So I moved what depends on the result of the async task into the onPostExecute() method of the async task, and voila... image displayed in my app widget!
Upvotes: 3