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

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

问题描述

我有一个应用程序小部件,其中仅包含一个imageview.我重新绘制该图像并将其作为png存储在应用程序的私有内存中,然后使用uri(widget.setImageViewUri(R.id.widget_icon,uri))设置RemoteViews的图像,而不是自行发送位图,因为这种情况非常少见我遇到的情况!!!绑定交易失败!!!

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 !!!

问题是,随着图像随时间的变化,窗口小部件的图像不会改变.我用root资源管理器检查了已保存的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天全站免登陆