应用小部件 setImageViewUri 不更新图像 [英] app widget setImageViewUri does not update the image

查看:30
本文介绍了应用小部件 setImageViewUri 不更新图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用小部件,它只包含一个图像视图.我重绘该图像并将其作为 png 存储在应用程序的私有内存中,然后我使用 uri (widget.setImageViewUri(R.id.widget_icon, uri)) 设置 RemoteViews 的图像,而不是自己发送位图,因为在非常罕见的情况下我得到的情况!Binder 交易失败!!!

i have an app widget, which contains only one imageview. i redraw that image and store it as png in apps's private memory and then i set the RemoteViews' image with the uri (widget.setImageViewUri(R.id.widget_icon, uri)) instead of sendding the bitmap it self, because in very rare situations i get !!! FAILED BINDER TRANSACTION !!!

问题是,随着图像随时间变化,小部件的图像不会改变.我用根资源管理器检查了保存的 png,它已正确更新.但未显示正确的图像.每个小部件都显示从它被添加到主屏幕时的图像.如果我使用 setImageViewBitmap(...) 它可以正常工作,除了罕见的失败的活页夹事务.

the problem is, that as the image changes over time, the widget's image does not change. i checked the saved png with root explorer and it is updated correctly. but the correct image is not displayed. each widget shows the image from the time it was added to the home screen. if i use setImageViewBitmap(...) it works correctly, except the rare failed binder transaction.

我使用以下方法从服务更新小部件.它不适用于 android 2.3.7 以及运行 2.2 的模拟器.可能是什么问题呢?它是否以某种方式缓存?

i update the widget from a service with the method below. it does not work on android 2.3.7 and also with emulator running 2.2. what could be the problem? is it somehow cached?

public void updateWidget(Context context) {
  // redraws the widget's image
  updateIcon();

  OutputStream stream;
  try {
    stream = context.openFileOutput("icon.png", Context.MODE_WORLD_READABLE);
  }
  catch (FileNotFoundException ex) {
    ex.printStackTrace();
    return;
  }

  m_Icon.compress(CompressFormat.PNG, 100, stream);

  try {
    stream.close();
  }
  catch (IOException ex) {
    ex.printStackTrace();
  }

  AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
  int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetProvider.class));

  Intent intent = new Intent(context, MyDialog.class);
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

  PendingIntent clickIntent = PendingIntent.getActivity(context, 0, intent, 0);

  File file = new File(context.getFilesDir().getAbsolutePath() + File.separator + "icon.png");
//      Uri uri = Uri.fromFile(file);

  // update all widgets on home screen
  for (int wid : appWidgetIds) {
    RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget);
    widget.setOnClickPendingIntent(R.id.widget_layout, clickIntent);

    // === EDIT ===
    // Uri.fromFile(file); does not work correctly on android 2.1, only 2.2 and up
    // widget's image will disappear
//        widget.setImageViewUri(R.id.widget_icon, uri);
    widget.setImageViewUri(R.id.widget_icon, Uri.parse(file.getPath()));

    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, wid);

    appWidgetManager.updateAppWidget(wid, widget);
  }
}

我还尝试了自定义 ContentProvider 并设置content://..."uri,但结果相同.小部件显示的是从添加到主屏幕时开始的图像,并且不会随着时间的推移而更新.

i also tried a custom ContentProvider and setting "content://..." uri, but with the same result. the widget shows the image from the time it was added to the home screen and does not update over time.

logcat 没有显示任何错误.

logcat does not show any errors.

推荐答案

如果 Uri 保持不变,Android 似乎不会更新图像.不过,我发现了一些技巧来解决这个问题.只需在 widget.setImageViewUri(R.id.widget_icon, uri); 之前调用 widget.setImageViewUri(R.id.widget_icon, Uri.parse(""));.

If the Uri remains unchanged, it seems Android won't update the image. I've found a bit of a hack to get around this though. Just call widget.setImageViewUri(R.id.widget_icon, Uri.parse("")); before widget.setImageViewUri(R.id.widget_icon, uri);.

它调用 setImageViewUri() 两次,但似乎工作正常.不过,我很想听听更好的方法来解决它,因为我自己也遇到了这个问题.

It calls setImageViewUri() twice, but seems to be working OK. I'd love to hear a better way to fix it though, as I'm experiencing this issue myself.

这篇关于应用小部件 setImageViewUri 不更新图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆