码迷,mamicode.com
首页 > 移动开发 > 详细

【Android】Android内存溢出问题---用自行开辟的空间进行对内存管理

时间:2014-06-07 23:47:53      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:des   android   c   style   class   blog   

bubuko.com,布布扣
    public static Bitmap readBitmap(String path) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inSampleSize = computeSampleSize(options, -1, 128 * 128);
        options.inJustDecodeBounds = true;
        // 自行在Android上开辟一段12K大小的内存空间,存储的图片放在该内存中
        options.inTempStorage = new byte[12 * 1024];
        // 获取资源图片
        InputStream is;
        try {
            is = new FileInputStream(path);
            return BitmapFactory.decodeStream(is, null, options);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

    }

    public static int computeSampleSize(BitmapFactory.Options options,
            int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength,
                maxNumOfPixels);

        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }

        return roundedSize;
    }

    private static int computeInitialSampleSize(BitmapFactory.Options options,
            int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;

        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
                .sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
                Math.floor(w / minSideLength), Math.floor(h / minSideLength));

        if (upperBound < lowerBound) {
            return lowerBound;
        }

        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }
bubuko.com,布布扣

 

【Android】Android内存溢出问题---用自行开辟的空间进行对内存管理,布布扣,bubuko.com

【Android】Android内存溢出问题---用自行开辟的空间进行对内存管理

标签:des   android   c   style   class   blog   

原文地址:http://www.cnblogs.com/niray/p/3774612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!