3

我正在尝试制作一个小部件,它可以从 sdcard 更改特定计时器中的图像。我的 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);
}

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

4

1 回答 1

0

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

如何在android中以编程方式更改应用小部件中的图像

于 2013-12-16T18:24:03.993 回答