【问题标题】:java.lang.out of memory error Android Bitmap Factoryjava.lang.out of memory error Android 位图工厂
【发布时间】:2014-06-14 17:31:19
【问题描述】:

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

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);

然后我将位图绘制到画布中..

这是错误:

06-14 14:21:16.452: W/dalvikvm(1930): threadid=1: 线程以未捕获的异常退出 (group=0xb1ab7ba8) 06-14 14:21:16.482: E/AndroidRuntime(1930): 致命异常: main 06-14 14:21:16.482:E/AndroidRuntime(1930):进程: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): 在 android.graphics.Bitmap.nativeCreate(Native Method) 06-14 14:21:16.482: E/AndroidRuntime(1930): 在 android.graphics.Bitmap.createBitmap(Bitmap.java:809) 06-14 14:21:16.482: E/AndroidRuntime(1930): 在 android.graphics.Bitmap.createBitmap(Bitmap.java:786) 06-14 14:21:16.482: E/AndroidRuntime(1930): 在 android.graphics.Bitmap.createBitmap(Bitmap.java:718) 06-14 14:21:16.482: E/AndroidRuntime(1930): 在 android.graphics.Bitmap.createScaledBitmap(Bitmap.java:594) 06-14 14:21:16.482: E/AndroidRuntime(1930): 在 com.hv.snake.MainMenu.onCreate(MainMenu.java:56) 06-14 14:21:16.482: E/AndroidRuntime(1930): 在 android.app.Activity.performCreate(Activity.java:5231) 06-14 14:21:16.482: E/AndroidRuntime(1930): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-14 14:21:16.482: E/AndroidRuntime(1930): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 06-14 14:21:16.482: E/AndroidRuntime(1930): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-14 14:21:16.482: E/AndroidRuntime(1930): 在 android.app.ActivityThread.access$800(ActivityThread.java:135) 06-14 14:21:16.482: E/AndroidRuntime(1930): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 06-14 14:21:16.482: E/AndroidRuntime(1930): 在 android.os.Handler.dispatchMessage(Handler.java:102) 06-14 14:21:16.482: E/AndroidRuntime(1930): 在 android.os.Looper.loop(Looper.java:136) 06-14 14:21:16.482: E/AndroidRuntime(1930): 在 android.app.ActivityThread.main(ActivityThread.java:5017) 06-14 14:21:16.482: E/AndroidRuntime(1930): 在 java.lang.reflect.Method.invokeNative(Native Method) 06-14 14:21:16.482: E/AndroidRuntime(1930): 在 java.lang.reflect.Method.invoke(Method.java:515) 06-14 14:21:16.482: E/AndroidRuntime(1930): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 06-14 14:21:16.482: E/AndroidRuntime(1930): 在 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)

【问题讨论】:

  • 我在第一个帖子中已经发布了错误,您现在可以看到它。
  • 什么是在线MainMenu.java:56?
  • pozadina = Bitmap.createScaledBitmap(pozadinap, s_width, s_height, false);
  • 显示完整的代码,包括回收位图的位置。
  • @user3330053 可能是你的宽度和高度太大了

标签: java android memory


【解决方案1】:

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

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

public void onDestroy() {      

    super.onDestroy();

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

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

【讨论】:

    猜你喜欢
    • 2011-03-31
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多