具有图像更改的Android小部件

标签 android image widget

我正在尝试制作一个小部件,它可以从 SD 卡更改特定计时器中的图像。我的 onUpdate 函数如下所示:

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
ComponentName thisWidget = new ComponentName(context, PhotoWidgetProvider.class);
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) 
    mExternalStorageWriteable = true;

if(mExternalStorageWriteable) {
    BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
    //of course it's a sample filename
String fileName = Environment.getExternalStorageDirectory().getPath() + "/DCIM/100MEDIA/IMAG0159.jpg";
Bitmap bitmap = BitmapFactory.decodeFile(fileName, options);
remoteViews.setImageViewBitmap(R.id.widget_imageview, bitmap);
    appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}

但是我在我的小部件上看不到任何照片。文件当然存在。

最佳答案

试试这个。经过一些修改后,它工作正常,对我来说更好。

How to change the image in app widget programmatically in android

关于具有图像更改的Android小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13528264/

相关文章:

flutter - 未调用 dispose 方法

android - 同时运行两个 React Native 服务器

android - 致命异常:AsyncTask#1使用JSON导致后台错误

Java,类的多个实例重置为最新创建的实例

css - 替代背景大小 : cover for img tags?

c++ - QT 小部件与 QT GUI

android - 如何处理来自同一供应商的许多软件包的 Android Market 推荐跟踪?

html - 如何堆叠两个 div 容器都响应地占用屏幕大小

java - 为什么 BufferedImage 不能正常工作?!!是因为我用错了吗?

ios - 重命名今日小部件标题