简体   繁体   中英

cache nor recycle help bitmap loading out-of-memory exception

I have code that loads bitmap:

    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) {}
    }

But I get this error:

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

I have tried

bitmap.recycle() --> i cannot recycle it as my drawableBitmap uses this bitmap.

adding cache layer + making it very small --> it still crashes.

what else would you suggest? How can I identify what exactly in my code causes the OOM error?

Obviously your cachePut(urlStr, bd); is not properly freein up recently cached images, You'll need to handle cache size, and ensure that its get garbage collected so that you get memory when you need it. Best option for that is week reference. Also google guava provides special cache tools that are using weak references.

Anyway if you decide not to use that, you'll need to handle cache cleanup manually...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM