在Android中解码小jpg图像时获取内存泄漏

时间:2014-01-15 11:55:32

标签: android memory bitmap imageview

我在我的应用中将小jpg图像解码为位图。

图像只有40K。

我使用这些方法:

public Bitmap decodeSampledBitmapFromResource(String pathName,int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(pathName, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    Bitmap bmp=BitmapFactory.decodeFile(pathName, options);
    return bmp;
}

public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }
    return inSampleSize;
}

当程序进入:

Bitmap bmp=BitmapFactory.decodeFile(pathName, options);
我在LogCat中获得了GC_Concurrent,

为什么?

1 个答案:

答案 0 :(得分:0)

GC_CONCURRENT本身并不错。它只是来自垃圾收集器的状态消息。图像解码涉及内存分配,看到垃圾收集器运行是正常的。