android 防止加载图片溢出

1.使用缩略图

 1         Options opt = new Options();
 2         opt.inJustDecodeBounds = true;
 3         int width = opt.outWidth;
 4         int height = opt.outHeight;
 5         
 6         Display d = getWindowManager().getDefaultDisplay();
 7         int screenWidth = d.getWidth();
 8         int screenHeight = d.getHeight();
 9         int scare = 1;
10         int sWidth = width/screenWidth;
11         int sHeight = height/screenHeight;
12         
13         if( sWidth >= sHeight && sWidth >= 1) {
14             scare = sWidth;  
15         } else  if( sWidth < sHeight && sHeight >=1 ){
16             scare = sHeight;
17         }
18         opt.inSampleSize = scare;
19         opt.inJustDecodeBounds = false;
20         Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/big.jpg");
21         iv.setImageBitmap(bm);

这个方法的确能够少占用不少内存,可是它的致命的缺点就是,因为加载的是缩略图,所以图片失真比较严重,对于对图片质量要求很高的应用,可以采用下面的方法。

2.一个叫做LruCache类专门用来做图片缓存处理的

http://blog.chinaunix.net/uid-26930580-id-4138306.html

        // 获取应用程序最大可用内存
        int maxMemory = (int) Runtime.getRuntime().maxMemory();
        int cachedSize = maxMemory/1000;

        mMemoryCache = new LruCache<String, Bitmap>(cachedSize){
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                // 重写此方法来衡量每张图片的大小,默认返回图片数量。
                return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
            }
        };
    public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
        if (getBitmapFromMemoryCache(key) == null) {
            mMemoryCache.put(key, bitmap);
        }
    }

    private Bitmap getBitmapFromMemoryCache(String url) {
        return mMemoryCache.get(url);
    }

3.软引用,

只要有足够的内存,就一直保持对象,直到发现内存吃紧且没有Strong Ref时才回收对象。

加载图片步骤:

  (1)先通过URL查看缓存中是否有图片,如果有,则直接去缓存中取得。

    如果没有,就开线程重新去网上下载。
      (2)下载完了之后,就把图片放在缓存里面,方便下次可以直接从缓存中取得。

    private Map<String, SoftReference<Bitmap>> imageMap 
                                               = new HashMap<String, SoftReference<Bitmap>>();
    public Bitmap loadBitmap(final String imageUrl,final ImageCallBack imageCallBack) {
            SoftReference<Bitmap> reference = imageMap.get(imageUrl);
            if(reference != null) {
                if(reference.get() != null) {
                    return reference.get();
                }
            }
            final Handler handler = new Handler() {
                public void handleMessage(final android.os.Message msg) {
                    //加入到缓存中
                    Bitmap bitmap = (Bitmap)msg.obj;
                    imageMap.put(imageUrl, new SoftReference<Bitmap>(bitmap));
                    if(imageCallBack != null) {
                        imageCallBack.getBitmap(bitmap);
                    }
                }
            };
            new Thread(){
                public void run() {
                    Message message = handler.obtainMessage();
                    message.obj = downloadBitmap(imageUrl);
                    handler.sendMessage(message);
                }
            }.start();
            return null ;
        }

        // 从网上下载图片
        private Bitmap downloadBitmap (String imageUrl) {
            Bitmap bitmap = null;
            try {
                bitmap = BitmapFactory.decodeStream(new URL(imageUrl).openStream());
                return bitmap ;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        public interface ImageCallBack{
            void getBitmap(Bitmap bitmap);
        }

http://blog.csdn.net/xiaanming/article/details/9825113

 

 

 

android屏幕适配方法 

http://www.cnblogs.com/manuosex/p/3661762.html

http://www.360doc.com/content/13/0409/11/7857928_277107102.shtml

 

 郭林专栏

http://blog.csdn.net/guolin_blog/article/details/9316683

 

 

Android堆内存也可自己定义大小

private final static int CWJ_HEAP_SIZE = 6* 1024* 1024 ;
VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE); //设置最小heap内存为6MB大小。当然对于内存吃紧来说还可以通过手动干涉GC去处理

 

 

 

防止内存泄露,溢出:

http://www.cnblogs.com/anee/archive/2011/03/15/2675874.html

 

 

 

 

7.http://www.android100.org/html/201308/04/3890.html

Android中如何查找内存泄露

posted @ 2015-07-15 23:09  firerdong  阅读(136)  评论(0编辑  收藏  举报