1

我厌倦了在 stackoverflow 中尝试每一种可能性并用谷歌搜索了很多......没有得到我想要的。

bt=BitmapFactory.decodeResource(getResources(), dressarray[indexdress],options);
//Background1 = getApplicationContext().getResources().getDrawable(dressarray[indexdress]);
bt1=Bitmap.createBitmap(bt);
dress.setImageBitmap(bt1);

我在各种情况下都有此代码...穿着代码是ImageView 我的屏幕上有很多图像..有10个ImageButtons,对于那些我有1个onClick方法可以ImageView通过检查按下哪个按钮来执行上述操作。

最后我调用了一个方法来回收位图(这是我在提问之前的最后一个变体)onClick。调用该方法是最后一行onClick

这是回收的方法

public void callrecycle()
    {
        bt.recycle();
        bt=null;
        bt1=null;
        System.gc();
    }

现在的问题是我有一个具有 HTC EVO 屏幕密度和分辨率的模拟器,平台 2.3.3 一个第二个模拟器,密度 180,分辨率 320X480 和平台 2.2

在 2.3.3 我得到一个错误

07-06 10:42:41.898: D/dalvikvm(420): GC_EXTERNAL_ALLOC freed <1K, 52% free 2611K/5379K, external 19034K/19732K, paused 55ms
07-06 10:42:41.918: E/dalvikvm-heap(420): 1382400-byte external allocation too large for this process.
07-06 10:42:42.028: I/dalvikvm-heap(420): Clamp target GC heap from 25.682MB to 24.000MB
07-06 10:42:42.028: E/GraphicsJNI(420): VM won't let us allocate 1382400 bytes
07-06 10:42:42.028: D/dalvikvm(420): GC_FOR_MALLOC freed <1K, 52% free 2611K/5379K, external 19034K/19732K, paused 35ms
07-06 10:42:42.028: D/AndroidRuntime(420): Shutting down VM
07-06 10:42:42.028: W/dalvikvm(420): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-06 10:42:42.050: E/AndroidRuntime(420): FATAL EXCEPTION: main
07-06 10:42:42.050: E/AndroidRuntime(420): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
07-06 10:42:42.050: E/AndroidRuntime(420):  at android.graphics.Bitmap.nativeCreate(Native Method)
07-06 10:42:42.050: E/AndroidRuntime(420):  at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
07-06 10:42:42.050: E/AndroidRuntime(420):  at android.graphics.Bitmap.createBitmap(Bitmap.java:432)
07-06 10:42:42.050: E/AndroidRuntime(420):  at android.graphics.Bitmap.createBitmap(Bitmap.java:383)
07-06 10:42:42.050: E/AndroidRuntime(420):  at android.graphics.Bitmap.createBitmap(Bitmap.java:367)
07-06 10:42:42.050: E/AndroidRuntime(420):  at com.internetdesignzone.dressup.DressUp$1.onClick(DressUp.java:334)
07-06 10:42:42.050: E/AndroidRuntime(420):  at android.view.View.performClick(View.java:2485)
07-06 10:42:42.050: E/AndroidRuntime(420):  at android.view.View$PerformClick.run(View.java:9080)
07-06 10:42:42.050: E/AndroidRuntime(420):  at android.os.Handler.handleCallback(Handler.java:587)
07-06 10:42:42.050: E/AndroidRuntime(420):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-06 10:42:42.050: E/AndroidRuntime(420):  at android.os.Looper.loop(Looper.java:123)
07-06 10:42:42.050: E/AndroidRuntime(420):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-06 10:42:42.050: E/AndroidRuntime(420):  at java.lang.reflect.Method.invokeNative(Native Method)
07-06 10:42:42.050: E/AndroidRuntime(420):  at java.lang.reflect.Method.invoke(Method.java:507)
07-06 10:42:42.050: E/AndroidRuntime(420):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-06 10:42:42.050: E/AndroidRuntime(420):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-06 10:42:42.050: E/AndroidRuntime(420):  at dalvik.system.NativeStart.main(Native Method)
07-06 10:42:44.968: I/Process(420): Sending signal. PID: 420 SIG: 9

并在 2.2 上使用相同的代码出现错误

07-06 10:35:37.929: D/dalvikvm(351): GC_EXTERNAL_ALLOC freed 3K, 52% free 2620K/5379K, external 6273K/7166K, paused 59ms
07-06 10:35:38.249: D/AndroidRuntime(351): Shutting down VM
07-06 10:35:38.271: W/dalvikvm(351): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-06 10:35:38.329: E/AndroidRuntime(351): FATAL EXCEPTION: main
07-06 10:35:38.329: E/AndroidRuntime(351): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@4051e4c8
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.graphics.Canvas.throwIfRecycled(Canvas.java:955)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.graphics.Canvas.drawBitmap(Canvas.java:1044)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:325)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.widget.ImageView.onDraw(ImageView.java:854)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.view.View.draw(View.java:6880)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.view.View.draw(View.java:6883)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.view.View.draw(View.java:6883)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.view.View.draw(View.java:6883)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-06 10:35:38.329: E/AndroidRuntime(351):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.view.ViewRoot.draw(ViewRoot.java:1522)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.os.Looper.loop(Looper.java:123)
07-06 10:35:38.329: E/AndroidRuntime(351):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-06 10:35:38.329: E/AndroidRuntime(351):  at java.lang.reflect.Method.invokeNative(Native Method)
07-06 10:35:38.329: E/AndroidRuntime(351):  at java.lang.reflect.Method.invoke(Method.java:507)
07-06 10:35:38.329: E/AndroidRuntime(351):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-06 10:35:38.329: E/AndroidRuntime(351):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-06 10:35:38.329: E/AndroidRuntime(351):  at dalvik.system.NativeStart.main(Native Method)
07-06 10:35:40.789: I/Process(351): Sending signal. PID: 351 SIG: 9

谁能告诉我原因?请告诉我如何重复使用回收的位图。我不想降低位图的质量。

我通过的选项有

inPurgeable=true
inInputShareable=true

注意:如果我不使用位图的东西并直接写dress.setImageResource(dressarray[indexdress]);

该应用程序像 2.2 中的冠军一样工作,但在第一次单击 2.3.3 时以超出预算关闭,dressarray[indexdress] 是包含 R.drawble.imgid 的图像数组,并且都是 PNG 图像

4

1 回答 1

0

我当时通过使用降级图像解决了这个问题。

您可以通过在选项中使用inSampleSize&来避免此错误。inPurgeable重复使用相同的Bitmap变量并null在每次使用后制作它。

System.gc()尽可能打电话。它不调用 gc,但可以启动它。

inPurgeable将使您的位图对象在执行时可清理gc()

inSampleSize将降级位图,因此请使用适当的值。

您也可以在创建时调整位图的大小,以减小其大小。采用createScaledBitmap()

希望对其他人有所帮助。

于 2013-01-23T08:20:22.110 回答