缩略可以用,但是也不能缩的太厉害,我现在是缩小了一般,就是decodeStream时候设置options的inSampleSize为至少为2,如果屏幕320*480时候就为3,但是模拟器还是出现了OOM

解决方案 »

  1.   

    其实没啥高见的,那么大尺寸的图片,到内存里只看分辨率大小,你可以考虑最终设备的RAM在2GB或4GB,一个图片占用率内存  为分辨率的长x宽x4 计算,你就明白了。回收内存用弱引用让JVM管理吧。
      

  2.   

    哎。。好久没来论坛了。
    这个问题。其实很简单。
    3部曲
    1:软引用。耗吧,我说这个基本没啥用。就是防止挂掉,还是看不了图片的。
    2:我还是放代码。public static Bitmap loadImageFromUrl(String url, int sc) {
    URL m;
    InputStream i = null;
    BufferedInputStream bis = null;
    ByteArrayOutputStream out = null;
    byte isBuffer[] = new byte[1024];
    if (url == null)
    return null;
    try {
    m = new URL(url);
    i = (InputStream) m.getContent(); bis = new BufferedInputStream(i, 1024 * 4);
    out = new ByteArrayOutputStream();
    int len = 0;
    while ((len = bis.read(isBuffer)) != -1) {
    out.write(isBuffer, 0, len);
    }
    out.close();
    bis.close();
    } catch (MalformedURLException e1) {
    e1.printStackTrace();
    return null;
    } catch (IOException e) {
    e.printStackTrace();
    }
    if (out == null)
    return null;
    byte[] data = out.toByteArray();
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(data, 0, data.length, options);
    options.inJustDecodeBounds = false;
    int be = (int) (options.outHeight / (float) sc);
    if (be <= 0) {
    be = 1;
    } else if (be > 3) {
    be = 3;
    }
    options.inSampleSize = be;
    Bitmap bmp = null;
    try {
    bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options); // 返回缩略图
    } catch (OutOfMemoryError e) {
    // TODO: handle exception
    System.gc();
    bmp = null;
    }
    return bmp;
    }3.暴力设置内存空间
    private final static float TARGET_HEAP_UTILIZATION = 0.75f;
    private final static int CWJ_HEAP_SIZE = 16 * 1024 * 1024;
    VMRuntime.getRuntime()
    .setTargetHeapUtilization(TARGET_HEAP_UTILIZATION);
    VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE);
      

  3.   

    这个好啊,复制收藏了。
    第一点,软引用。之前也是一直用,应该是在内存不足的时候被jvm强制回收是吧。后来发现明明内存还有很多,就是远没到内存不足的情况,bitmap还是被回收了,后来看某博主说Android在2.2之后,软引用和弱引用一样,只要gc就没得了。所以确实感觉没啥效果,经常滑着滑着又去网络加载图片
    第二点,核心是压缩图片,降低图片质量把。最后那个try catch很给力啊,哈哈
    第三点,没有试过,不知道有没有什么后遗症!!
      

  4.   

    我感觉就算下载到本地加载的时候一样会出现OOM错误啊?这个我始终不知道该怎么解决,我也遇上这种问题。我的图片还少,才十多张。但是程序需要一次全部生成bitmap,然后就出现错误了。我还没法用完一张回收一张。但是不知道为什么在模拟器上面有错误,在我的手机上就没有错误。android分的不都是8M 的内存吗?不理解。
      

  5.   

    “保证”只能用在静物比较适合,Android的兼容性是一个超大的问题。
      

  6.   

    求skia.os,编译通不过呀,求大神眷顾。
      

  7.   

    http://blog.csdn.net/liliang497/article/details/7305257
      

  8.   

    推荐第三方库 universalimageloader