gpt4 book ai didi

java.lang.out of memory 错误 Android Bitmap Factory

转载 作者:行者123 更新时间:2023-11-30 11:17:44 25 4
gpt4 key购买 nike

我尝试回收位图和我在谷歌上找到的所有其他东西,但问题仍然存在...这是我的代码:

final SampleView mainPogled = new SampleView(this);

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Config.RGB_565;
options.inDither = true;

pozadinap = BitmapFactory.decodeResource(getResources(), R.drawable.nivoapozadina, options);
playButtonp = BitmapFactory.decodeResource(getResources(), R.drawable.play, options);

pozadina = Bitmap.createScaledBitmap(pozadinap, s_width, s_height, false);
playButton = Bitmap.createBitmap(playButtonp);

然后我将位图绘制到 Canvas 中..

这里是错误:

06-14 14:21:16.452: W/dalvikvm(1930): threadid=1: thread exiting with uncaught exception (group=0xb1ab7ba8) 06-14 14:21:16.482: E/AndroidRuntime(1930): FATAL EXCEPTION: main 06-14 14:21:16.482: E/AndroidRuntime(1930): Process: com.hv.snake, PID: 1930 06-14 14:21:16.482: E/AndroidRuntime(1930): java.lang.OutOfMemoryError 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.graphics.Bitmap.nativeCreate(Native Method) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.graphics.Bitmap.createBitmap(Bitmap.java:809) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.graphics.Bitmap.createBitmap(Bitmap.java:786) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.graphics.Bitmap.createBitmap(Bitmap.java:718) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:594) 06-14 14:21:16.482: E/AndroidRuntime(1930): at com.hv.snake.MainMenu.onCreate(MainMenu.java:56) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.app.Activity.performCreate(Activity.java:5231) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.app.ActivityThread.access$800(ActivityThread.java:135) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.os.Handler.dispatchMessage(Handler.java:102) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.os.Looper.loop(Looper.java:136) 06-14 14:21:16.482: E/AndroidRuntime(1930): at android.app.ActivityThread.main(ActivityThread.java:5017) 06-14 14:21:16.482: E/AndroidRuntime(1930): at java.lang.reflect.Method.invokeNative(Native Method) 06-14 14:21:16.482: E/AndroidRuntime(1930): at java.lang.reflect.Method.invoke(Method.java:515) 06-14 14:21:16.482: E/AndroidRuntime(1930): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 06-14 14:21:16.482: E/AndroidRuntime(1930): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-14 14:21:16.482: E/AndroidRuntime(1930): at dalvik.system.NativeStart.main(Native Method)

最佳答案

我在 2 天后找到了解决方案... This这篇文章对我帮助很大。

在 onDestroy() 方法中,我首先从 Activity 中回收所有位图,然后将它们全部设置为 null,然后调用 System.gc()。例如

public void onDestroy() {      

super.onDestroy();

background.recycle();
background = null;
System.gc();
}

我希望这会对某人有所帮助。

关于java.lang.out of memory 错误 Android Bitmap Factory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24222507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com