Search code examples
androidbitmapandroid-widgetwidgetsd-card

Widget loses its bitmap background when SD-card is mounted / unmounted


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.


Solution

  • 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.