我试图制作一个小部件,它可以从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);
}
但是在我的小部件上看不到任何照片。文件当然存在。
答案 0 :(得分:0)
试试这个。经过一些修改后,它的工作正常,对我来说更好。
How to change the image in app widget programmatically in android