安装/卸载 SD 卡时,小部件会丢失其位图背景
我的应用程序是一个小部件,其背景是 imageView。 在 WidgetProvider 的 onUpdate() 方法上,我将 sdcard 中的位图放入调用此方法的 imageView 中:
public static void changeSkinWidget(Context context,String imageBackgroundPath) throws IOException{
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.widget);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Log.i(Constants.DebugTag,"changeSkinWidget receive new path " +imageBackgroundPath);
//Change Background Skin :
FileInputStream is;
try {
is = new FileInputStream(new File(imageBackgroundPath
+ "background.png"));
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(is);
views.setImageViewBitmap(R.id.ImageBackground01, bm);
ComponentName thisWidget = new ComponentName(context,
MyWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget, views);
bis.close();
is.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
问题是我总是安装/卸载 SDCARD,小部件会丢失此位图,并且 ImageView 会获取布局声明中找到的默认可绘制对象( src=R.drawable.defaultbackground ),看来我的小部件已重新加载。 为了跟踪这一点,我使用了 onReceive() 方法: Log.i(Constants.DEBUG_TAG,"接收意图:"+intent.getAction()); 但 logcat 中没有出现任何内容...我预计可能是 ACTION_APPWIDGET_UPDATE 的意图 行动,但什么也没有。
我还尝试对我的位图调用 recycle() ...这并不能解决问题。
在安装/卸载时观察 logcat 时我没有收到任何奇怪的错误...
任何帮助将不胜感激! 克里斯托夫.
My app is a widget, which background is an imageView.
On the onUpdate() method of the WidgetProvider, I put a bitmap from sdcard inside this imageView calling this method :
public static void changeSkinWidget(Context context,String imageBackgroundPath) throws IOException{
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.widget);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Log.i(Constants.DebugTag,"changeSkinWidget receive new path " +imageBackgroundPath);
//Change Background Skin :
FileInputStream is;
try {
is = new FileInputStream(new File(imageBackgroundPath
+ "background.png"));
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(is);
views.setImageViewBitmap(R.id.ImageBackground01, bm);
ComponentName thisWidget = new ComponentName(context,
MyWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget, views);
bis.close();
is.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The problem is that always I mount/unmount SDCARD, the widget loses this bitmap, and ImageView get the defaut drawable found in layout declaraction ( src=R.drawable.defaultbackground) and it seems my widget is reloaded.
To track this I put on onReceive() method :
Log.i(Constants.DEBUG_TAG,"RECEIVE INTENT : "+intent.getAction());
But nothing appears in logcat...I was expected maybe a intent with ACTION_APPWIDGET_UPDATE
action, but nothing.
I had also tried to call recycle() to my bitmap...this does not solve the problem.
I don't get any strange error watching to logcat while mounting/unmounting ...
Any Help would be appreciate !
Christophe.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案在此处
与实际解决方案无关的其他注释...
与绝不应将小部件移至 SD 卡类似,小部件也不应引用 SD 卡上的资源。小部件可以经常重新加载,并且它们需要访问该资源。一种可能的解决方案是将图像文件存储在本地文件系统的 widgets /data 目录中。
Solution is here
Misc notes unrelated to actual solution...
Similarly to how widgets should never be moved to the SD card, widgets should also not reference resources on the SD card. Widgets can be reloaded often and they will need acces to that resource. A possible solution is to store the image file on the local file system in the widgets /data directory.