小部件中的图像

8
我正在尝试在 onUpdate 中在小部件布局中设置图像到图像视图,但是图像没有更新。
 @Override
     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
     {
         Log.i(TAG, "onUpdate called");
         for (int appWidgetId : appWidgetIds) 
         {
             Bitmap bitmap=ImageUtils.getBitmap(context, appWidgetId);
             RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget);

             remoteView.setImageViewBitmap(R.id.imgView,bitmap);
             appWidgetManager.updateAppWidget(appWidgetId, remoteView);
         }
     }

小部件的XML布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imgView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

清单文件
<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.demo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity
             android:name=".WidgetConfig"
             android:label="@string/app_name" >
             <intent-filter>
                  <action
                    android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
             </intent-filter>
         </activity>
        <receiver
            android:name="BasicWidgetProvider">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_info" />
        </receiver>

          <receiver
            android:name="BasicWidgetProvider">
            <intent-filter>
                <action
                    android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                    <data android:scheme="widget" />
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_info" />
        </receiver>
    </application>

widget_info.xml

<appwidget-provider 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="25dp"
    android:minHeight="25dp"
    android:initialLayout="@layout/widget"
    android:configure="com.demo.WidgetConfig" 
    android:widgetCategory="home_screen|keyguard" 
    android:previewImage="@layout/widget" 
    android:resizeMode="horizontal|vertical" 
    android:initialKeyguardLayout="@layout/widget" 
    android:minResizeHeight="5dp" 
    android:minResizeWidth="5dp">
</appwidget-provider>

我尝试加载小部件时,却弹出了一个“应用未安装”的Toast提示,但我并没有显示这样的Toast,为什么会出现呢?

图片为什么没有设置好,怎样修复它?


它没有图片可以加载吗?对我来说,这听起来像是小部件/应用程序配置方面出了问题,而不是图片。您能发布清单吗? - Gustek
我添加了清单文件,请查看代码。 - Akhil Jain
3个回答

1
请确保在appwidget-provider xml文件中的"android:configure"字段中设置了正确的包名,并且在您的清单文件中设置了正确的intent-filter。

他说的是小部件提供程序XML文件,而不是布局。 - JoxTraex
好的,我在问题的评论区发布了评论,并在此处进行了回复,这是我的错误,我会再次更新。 - Akhil Jain

0
请尝试使用以下类似的remoteview.setimageViewuri。
@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
        thisWidget = new ComponentName(context, HelloWidget.class);
        remoteViews.setImageViewUri(R.id.imageView1,Uri.parse("/mnt/sdcard/test.png")); 
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);
    }

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

你的名字前面缺少点号,而且你有两个相同的接收器。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接