【问题标题】:OpenGL random crash on Honeycomb with Hardware Accelerated param set to trueHoneycomb 上的 OpenGL 随机崩溃,硬件加速参数设置为 true
【发布时间】:2011-06-22 08:31:34
【问题描述】:

我在使用 Android Honeycomb 3.0 (Acer Iconia) 时遇到了一个大问题。 我没有使用任何 openGL 视图,我只在清单中设置

android:hardwareAccelerated="true"

事实是,当我加载具有一定数量图片的视图时,我在单击按钮时多次更改其内容和背景,我的意图完成并出现以前的意图。我认为我很好地管理了记忆。此消息在附加时出现:

06-22 10:13:53.510: ERROR/libEGL(951): call to OpenGL ES API with no current context (logged once per thread)
06-22 10:14:03.150: ERROR/InputDispatcher(113): channel '4098b530 com.mycompany.myapp/com.mycompany.myapp.FolderActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
06-22 10:14:03.150: ERROR/InputDispatcher(113): channel '4098b530 com.mycompany.myapp/com.mycompany.myapp.FolderActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
06-22 10:14:03.150: ERROR/InputDispatcher(113): channel '409d30e0 com.mycompany.myapp/mycompany.myapp.GalleryActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
06-22 10:14:03.150: ERROR/InputDispatcher(113): channel '409d30e0 com.mycompany.myapp/mycompany.myapp.GalleryActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

当我设置这个时,我没有崩溃,但它非常滞后:

android:hardwareAccelerated="false"

【问题讨论】:

    标签: android android-3.0-honeycomb hardware-acceleration


    【解决方案1】:

    好的,伙计们,我找到了解决方案 :) 我将标签更新到 Android 3.1,这给了我真正的错误,OutOfMemoryError。我在单击按钮时分配背景。所以我现在做一个

    WeakReference<BitmapDrawable>
    

    而不是 位图可绘制 并禁用我的按钮,直到背景设置好(以避免同时创建两个大的 BitmapDrawable)。

    希望对你有帮助,可怜的 3.0 开发者^^

    编辑:这是我创建的用于管理缓存的类:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.lang.ref.WeakReference;
    import java.util.WeakHashMap;
    
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.BitmapFactory.Options;
    import android.graphics.drawable.BitmapDrawable;
    
    public class BitmapDrawableCache {
    
        private static BitmapDrawableCache                 instance = null;
        WeakHashMap<String, WeakReference<BitmapDrawable>> cache;
    
        private BitmapDrawableCache() {
            cache = new WeakHashMap<String, WeakReference<BitmapDrawable>>();
        }
    
        public synchronized static BitmapDrawable get(String drawablePath) {
            if (instance == null) {
                instance = new BitmapDrawableCache();
            }
    
            BitmapDrawable bmd = null;
            WeakReference<BitmapDrawable> wr0 = instance.cache.get(drawablePath);
            if (instance.cache.get(drawablePath) != null) bmd = wr0.get();
    
            if (bmd == null) {
                WeakReference<BitmapDrawable> wr = new WeakReference<BitmapDrawable>(new BitmapDrawable(drawablePath));
                instance.cache.put(drawablePath, wr);
                bmd = wr.get();
            }
    
            return bmd;
    
        }
    
        public synchronized static BitmapDrawable get(Bitmap bitmap) {
    
            String drawableName = "_bitmap_" + bitmap.hashCode();
    
            if (instance == null) {
                instance = new BitmapDrawableCache();
            }
    
            BitmapDrawable bmd = null;
            WeakReference<BitmapDrawable> wr0 = instance.cache.get(drawableName);
            if (instance.cache.get(drawableName) != null) bmd = wr0.get();
    
            if (bmd == null) {
                WeakReference<BitmapDrawable> wr = new WeakReference<BitmapDrawable>(new BitmapDrawable(bitmap));
                instance.cache.put(drawableName, wr);
                bmd = wr.get();
            }
    
            return bmd;
    
        }
    
    
        //Get BitmapDrawable in cache with options to reduce its size
        public synchronized static BitmapDrawable get(String drawableName, Options options) {
            if (instance == null) {
                instance = new BitmapDrawableCache();
            }
    
            BitmapDrawable bmd = null;
            WeakReference<BitmapDrawable> wr0 = instance.cache.get(drawableName);
            if (instance.cache.get(drawableName) != null) bmd = wr0.get();
    
            if (bmd == null) {
                File f = new File(drawableName);
                WeakReference<BitmapDrawable> wr = new WeakReference<BitmapDrawable>(new BitmapDrawable(instance.decodeFile(f, options)));
                instance.cache.put(drawableName, wr);
                bmd = wr.get();
            }
    
            return bmd;
    
        }  
    
    
      //decodes image and scales it to reduce memory consumption
        private Bitmap decodeFile(File f, Options o2){
            try {
                //Decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(new FileInputStream(f),null,o);
    
                int scale=1;
                //The new size we want to scale to
                final int REQUIRED_SIZE=70;
    
                //Find the correct scale value. It should be the power of 2.
                int width_tmp=o.outWidth, height_tmp=o.outHeight;
                while(true){
                    if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                        break;
                    width_tmp/=2;
                    height_tmp/=2;
                    scale*=2;
                }
    
                //Decode with inSampleSize
               // BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize=scale;
                return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
            } catch (FileNotFoundException e) {}
            return null;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      • 2013-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多