1

我有活动 A、B 和 C。它们都是 setContentView,其中 XML 使用可绘制的 aa 背景。我将我的背景可绘制图像放在 drawable-nodpi 文件夹中。

但是,每隔一段时间,我就会在活动 B 的崩溃报告中得到以下异常

java.lang.OutOfMemoryError: bitmap size exceeds VM budget
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:563)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:439)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
    at android.content.res.Resources.loadDrawable(Resources.java:1981)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:653)
    at android.view.View.<init>(View.java:1961)
    at android.view.View.<init>(View.java:1909)
    at android.view.ViewGroup.<init>(ViewGroup.java:286)
    at android.widget.LinearLayout.<init>(LinearLayout.java:120)
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
    at android.view.LayoutInflater.createView(LayoutInflater.java:505)
    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:215)
    at android.app.Activity.setContentView(Activity.java:1663)
    at com.mypackage.myapp.ActivityB.onCreate

可绘制的背景具体为 67 KB,大小为 1122 x 1682 像素。

我该怎么办?我什至无法在我的手机上复制它

4

1 回答 1

0

这种方法会做采样。您可以在我的代码中修改采样率,即 inSampleSize。

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 = 2;

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

// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);

// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}

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);
}

最后在您的 Imageview 或任何其他视图中设置此采样图像。使用这个

mImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(),R.id.myimage,    
Screen_width, screen_height));
于 2013-09-10T17:51:50.020 回答