2010-08-23 3 views
2

mon application a des widgets qui ont un ImageView et un TextView. Sur la méthode de la WidgetProvider, je mets un Bitmap dans la ImageView cette façon onUpdate(): méthodeRuntimeException sur le widget qui a un bitmap (Android)

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.widget_btn); 
Bitmap bitmap2 = BitmapManager.setColor(bitmap, red, green, blue); 
views.setImageViewBitmap(R.id.image, bitmap2); 

setColor() est la suivante:

public synchronized static Bitmap setColor(Bitmap org, float r, float g, float b) 
{ 
    sColorMatrix.setScale(r, g, b, 1); 
    sColorPaint.setColorFilter(new ColorMatrixColorFilter(sColorMatrix)); 
    // RGB_565 is faster, but loses transparency 
    Bitmap ret = Bitmap.createBitmap(org.getWidth(), org.getHeight(), Bitmap.Config.ARGB_8888); 
    try{ 
     sColorCanvas.setBitmap(ret); 
     //sColorCanvas.drawColor(Color.); 
     sColorCanvas.drawBitmap(org, 0, 0, sColorPaint); 
    } catch (Throwable t){ 

    } 

    return ret; 
} 

Le problème est que parfois le widget lance une RuntimeException parce que quelqu'un a recyclé le bitmap, et je ne sais pas quoi faire. Quelques suggestions?

Je peux attacher la pile si cela peut être utile. Je vous remercie!


C'est le stacktrace:

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap [email protected] 
    at android.graphics.Canvas.throwIfRecycled(Canvas.java:955) 
    at android.graphics.Canvas.drawBitmap(Canvas.java:1044) 
    at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:323) 
    at android.widget.ImageView.onDraw(ImageView.java:923) 
    at android.view.View.draw(View.java:6739) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:1648) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375) 
    at android.widget.AbsListView.dispatchDraw(AbsListView.java:1365) 
    at android.widget.ListView.dispatchDraw(ListView.java:3046) 
    at android.view.View.draw(View.java:6845) 
    at android.widget.AbsListView.draw(AbsListView.java:2257) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:1648) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375) 
    at android.view.View.draw(View.java:6742) 
    at android.widget.FrameLayout.draw(FrameLayout.java:352) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:1648) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375) 
    at android.view.View.draw(View.java:6742) 
    at android.widget.FrameLayout.draw(FrameLayout.java:352) 
    at android.view.ViewGroup.drawChild(ViewGroup.java:1648) 
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375) 
    at android.view.View.draw(View.java:6742) 
    at android.widget.FrameLayout.draw(FrameLayout.java:352) 
    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1872) 
    at android.view.ViewRoot.draw(ViewRoot.java:1422) 
    at android.view.ViewRoot.performTraversals(ViewRoot.java:1167) 
    at android.view.ViewRoot.handleMessage(ViewRoot.java:1744) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:144) 
    at android.app.ActivityThread.main(ActivityThread.java:4937) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:521) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
    at dalvik.system.NativeStart.main(Native Method) 

Répondre

0

La solution est de supprimer tous les appels à Bitmap#recycle de votre code. Le garbage collector prendra soin des bitmaps non référencés.

+0

Salut, je n'ai pas appelé recycle() sur aucune partie de mon code, c'est la chose curieuse ... Merci! – gskbyte

+0

C'est très bizarre. 'recycle' doit être explicitement appelé. Je ne vois rien qui me saute dessus quand je fais une hiérarchie d'appels non plus. – Qberticus

+0

Si le recyclage est supprimé, cela peut générer un problème de mémoire insuffisante. – Namratha

0

Il semble que le bitmap2 soit hors de portée et qu'il reçoive la valeur GC'd alors, quand il est temps, il a disparu.

Faites de bitmap2 un champ statique. Cela devrait arrêter sa collecte jusqu'à ce que la classe contenant soit détruite.