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

Java 缓存或回收帮助位图加载内存不足异常,java,android,memory-management,bitmap,out-of-memory,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

我有加载位图的代码:

    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错误的确切原因?

显然是您的缓存(urlStr,bd);无法正确释放最近缓存的图像, 您需要处理缓存大小,并确保其已被垃圾回收,以便在需要时获得内存。最好的选择是周参考。google guava还提供了使用弱引用的特殊缓存工具


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

这与我在StackOverflow上给多个用户的建议相同。最好的办法是避免重新发明轮子。图像加载在Android上是一个常见(而且非常复杂)的问题,有一些非常好的库。我最喜欢的是:
Picasso.with(context).load(uri).into(imgView)它为您完成所有缓存和内存管理。