使用BitmapFactory.decodeResource时获得增长堆

时间:2017-06-15 13:08:45

标签: android

我收到以下警告:

Grow heap (frag case) to 15.723MB for 2073616-byte allocation

我正在使用此功能(见下文)将drawable保存到文件中。

有更好的方法来保存图像吗?

public void save(int resId) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = IMAGE_IN_SAMPLE_SIZE;
    options.inScaled = false;
    options.inJustDecodeBounds = false;

    Bitmap bitmapImage = BitmapFactory.decodeResource(context.getResources(), resId, options);

    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(createFile());
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个..添加到AndroidManifest.xml中的applciation标签

 android:largeHeap="true"