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

标签 java android memory

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

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/

相关文章:

java - Matlab 函数 'quad' 在 Java 和 C++ 中可用吗?

java - 相同的值多次保存到数据库中,即使 hibernate 中存在另一个列表

Android - 获取我的应用程序中选定的 pdf、doc、ppt 或 xls 的文件路径

android - 如何在不显示通知的情况下 startForeground()?

c++ - 简单的结构对齐/填充问题

java - Android 中 BitmapFactory.decodeByteArray 返回 null 且 inJustDecoteBounds=true

android - 应用程序启动器文本 - 两行应用程序名称

c++ - 如何使用 "new"分配函数内点引用参数的内存

c - 动态内存分配的流行用法

java - 如何在手机上运行java应用程序?