如何将图像设置为墙纸而不会导致OutOfMemory错误

时间:2014-07-21 17:42:13

标签: java android out-of-memory

我有一个直接的壁纸应用程序,我刚刚完成编码。我在这里使用了教程(http://developer.android.com/training/displaying-bitmaps/load-bitmap.html)以较低的分辨率在屏幕上加载图像,而我使用下面的代码设置壁纸。 imageId代表当前选定的drawable。

InputStream is = getResources().openRawResource(imageId);
Bitmap bitmap = BitmapFactory.decodeStream(is);
WallpaperManager myWallpaperManager = WallpaperManager
        .getInstance(getApplicationContext());

try {
    myWallpaperManager.setBitmap(bitmap);
    Toast.makeText(MainActivity.this, "Teamwork! Wallpaper set.",
            Toast.LENGTH_LONG).show();
} catch (IOException e) {
    Toast.makeText(
            MainActivity.this,
            "Damnit, clickers! Wallpaper wasn't set. Try killing and restarting the app?",
            Toast.LENGTH_LONG).show();
}  

但是,我担心上面的代码可能会导致内存较少的手机在调用时崩溃。我能做些什么来防止这种情况发生,还是没有什么可担心的?

编辑:

我使用此代码在应用中显示图片:

public static 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;
}

public static Bitmap decodeSampledBitmapFromResource(Resources res,
        int resId, int reqWidth, int reqHeight) {

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

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

public void setImage() {

    switch (imageNum) {

    case 1:

        options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(), R.drawable.aaaae,
                options);
        display.setImageBitmap(decodeSampledBitmapFromResource(
                getResources(), R.drawable.aaaae, 300, 300));

        imageId = R.drawable.aaaae;

        break;

// Etc, etc.
    }
}

每次" Next"时都会调用setImage方法。按下按钮。

1 个答案:

答案 0 :(得分:1)

我猜你没有正确处理图像

这是处理android中位图的解决方案 (这种方法可以在非ui线程中处理25000像素×25000像素的图像。)

首先,您应该计算位图的sampleBitmapSize(以加载相同位图的较低版本)

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
    int reqWidth, int reqHeight) {

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

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

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);

} 下面是用于计算InSampleSize的util函数(一个定义位图质量值(等级)的整数)。

public static 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;
    }
}

Log.i("ImageUtil", "InSampleSize: "+inSampleSize);
return inSampleSize;

}