位图的大小超过VM预算的具体活动 [英] bitmap size exceeds VM budget for specific activity
问题描述
我有活性的A,B和C,他们中的XML使用AA绘制背景的所有的setContentView。我把绘制-nodpi文件夹我的背景绘制的图像。
I have activity A, B and C. They all setContentView in which the XML uses a drawable a a background. I put my background drawable image in drawable-nodpi folder.
不过,每过一段时间,我得到了在b活动特别
However, every once in a while, I get the following exception in crash report in activity B specifically
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由1682像素。
The background drawable for that specifically is 67 KB and it is 1122 by 1682 pixels.
我应该怎么办?我甚至无法重现它在我的手机
What should I do? I can't even reproduce it on my phone
推荐答案
此方法会做抽样。您可以修改采样率,即inSampleSize在我的code。
This method Will do the sampling . You can modify sampling rate i.e. inSampleSize in my code.
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或任何其他视图。使用这种
And at the last set this sampled Image in your Imageview or any another view. Using this
mImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(),R.id.myimage,
Screen_width, screen_height));
这篇关于位图的大小超过VM预算的具体活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!