缓存或回收帮助位图加载内存不足异常

时间:2014-06-02 14:36:39

标签: java android memory-management bitmap out-of-memory

我有加载位图的代码:

    public void run() {
      final Bitmap bitmap;
      try {
        bitmap = MediaStore.Images.Media.getBitmap(ab.getContentResolver(), tryUri);
        BitmapDrawable bd = new BitmapDrawable(ab.getResources(), bitmap);
        cachePut(urlStr, bd);

        ab.runOnUiThread(new Runnable() {
          @Override
          public void run() {
            // make sure the tag is still the one we set at the beginning of this function
            if (toSet.getTag() == urlStr) {
              toSet.setImageBitmap(bitmap);
            }
          }
        });
      } catch (FileNotFoundException e) {} catch (IOException e) {}
    }

但是我收到了这个错误:

06-02 17:30:16.221: E/AndroidRuntime(30889): FATAL EXCEPTION: Thread-54671
06-02 17:30:16.221: E/AndroidRuntime(30889): java.lang.OutOfMemoryError
06-02 17:30:16.221: E/AndroidRuntime(30889):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
06-02 17:30:16.221: E/AndroidRuntime(30889):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
06-02 17:30:16.221: E/AndroidRuntime(30889):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:696)
06-02 17:30:16.221: E/AndroidRuntime(30889):    at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:814)
06-02 17:30:16.221: E/AndroidRuntime(30889):    at com.m.utils.ImageRepository$3.run(ImageRepository.java:180)
06-02 17:30:16.221: E/AndroidRuntime(30889):    at java.lang.Thread.run(Thread.java:841)
06-02 17:30:16.671: E/android.os.Debug(2395): !@Dumpstate > sdumpstate -k -t -z -d -o /data/log/dumpstate_app_error
06-02 17:30:17.826: E/ViewRootImpl(2395): sendUserActionEvent() mView == null
06-02 17:30:18.146: E/Sensors(2395): AkmSensor : handle 1, en : 0 
06-02 17:30:18.146: E/Sensors(2395): MagSensor old sensor_state 15, new sensor_state : 11 en : 0
06-02 17:30:18.151: E/InputDispatcher(2395): channel ~ Channel is unrecoverably broken and will be disposed!
06-02 17:30:18.151: E/InputDispatcher(2395): channel ~ Channel is unrecoverably broken and will be disposed!
06-02 17:30:18.211: E/SELinux(32227): Function: selinux_android_load_priority [0], There is no sepolicy file 
06-02 17:30:18.211: E/SELinux(32227):  
06-02 17:30:18.216: E/SELinux(32227): Function: selinux_android_load_priority , loading version is VE=SEPF_GT-N7100_4.3_0018
06-02 17:30:18.216: E/SELinux(32227):  
06-02 17:30:18.216: E/SELinux(32227):  
06-02 17:30:18.216: E/SELinux(32227): selinux_android_seapp_context_reload: seapp_contexts file is loaded from /data/security/spota/seapp_contexts
06-02 17:30:18.401: E/AndroidRuntime(32227): FATAL EXCEPTION: main
06-02 17:30:18.401: E/AndroidRuntime(32227): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.m/com.m.navigate.social.MyFriendsActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class com.m.view.title.TitleButtonBar
06-02 17:30:18.401: E/AndroidRuntime(32227):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
06-02 17:30:18.401: E/AndroidRuntime(32227):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
06-02 17:30:18.401: E/AndroidRuntime(32227):    at android.app.ActivityThread.access$700(ActivityThread.java:168)
06-02 17:30:18.401: E/AndroidRuntime(32227):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
06-02 17:30:18.401: E/AndroidRuntime(32227):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 17:30:18.401: E/AndroidRuntime(32227):    at android.os.Looper.loop(Looper.java:137)
06-02 17:30:18.401: E/AndroidRuntime(32227):    at android.app.ActivityThread.main(ActivityThread.java:5493)
06-02 17:30:18.401: E/AndroidRuntime(32227):    at java.lang.reflect.Method.invokeNative(Native Method)
06-02 17:30:18.401: E/AndroidRuntime(32227):    at java.lang.reflect.Method.invoke(Method.java:525)
06-02 17:30:18.401: E/AndroidRuntime(32227):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
06-02 17:30:18.401: E/AndroidRuntime(32227):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
06-02 17:30:18.401: E/AndroidRuntime(32227):    at dalvik.system.NativeStart.main(Native Method)
06-02 17:30:18.401: E/AndroidRuntime(32227): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.m.view.title.TitleButtonBar

我试过了

bitmap.recycle() - >我无法回收它,因为我的drawableBitmap使用此位图。

添加缓存层+使其非常小 - >它仍然崩溃。

你还有什么建议? 如何识别代码中的确切原因导致OOM错误?

1 个答案:

答案 0 :(得分:0)

显然你的cachePut(urlStr,bd);最近没有正确释放缓存的图像, 您需要处理缓存大小,并确保收集垃圾,以便在需要时获取内存。最佳选择是周参考。此外,谷歌番石榴提供了使用弱引用的特殊缓存工具。

无论如何,如果你决定不使用它,你需要手动处理缓存清理......