我正在为Android开发一款纸牌游戏。当我尝试加载一些图像时,我有一个OutOfMemoryError。我已经阅读了许多关于android中常见和常见问题的答案,但是当加载的图像非常大时,所有这些问题都会解决。我自己管理屏幕旋转,所以我不重新开始轮换的新活动。 我已经读过这些问题了:
Out of memory exception due to large bitmap size
Android: out of memory exception in Gallery
Android handling out of memory exception on image processing
我不明白的一件事是,如果res文件夹中所有图像(png文件)的大小总和大约为800 kb。但是在Logcat中我注意到创建游戏(加载卡片图像)的大小快速超过20 mb。 我用:这段代码加载图片
Resources r = this.getContext().getResources();
CardView.loadCardImages(CardView.EAlone.CARDS, 1027, 615, r.getDrawable(R.drawable.cards));
CardView.loadCardImages(CardView.EAlone.GREEN, mCardWidth, mCardHeight, r.getDrawable(R.drawable.green));
CardView.loadCardImages(CardView.EAlone.RED, mCardWidth, mCardHeight, r.getDrawable(R.drawable.red));
CardView.loadCardImages(CardView.EAlone.WHITE, mCardWidth, mCardHeight, r.getDrawable(R.drawable.white));
CardView.LoadCardImages:
public static void loadCardImages(EAlone mode, int widthPixels, int heightPixels, Drawable tile) {
Canvas canvas;
switch (mode)
{
case GREEN:
mAloneGreen = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
canvas = new Canvas(mAloneGreen);
break;
case RED:
mAloneRed = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
canvas = new Canvas(mAloneRed);
break;
case WHITE:
mAloneWhite = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
canvas = new Canvas(mAloneWhite);
break;
default:
mcardImages = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
canvas = new Canvas(mcardImages);
}
tile.setBounds(0, 0, widthPixels, heightPixels);
tile.draw(canvas);
tile.setCallback(null);
}
mCardimages包含所有卡面。然后我使用此文件通过从文件中裁剪所需的卡来创建单个卡。 在这次加载之后,我在父视图中加载了一些其他具有透明度的图像。
dealerSign = new ImageView(getContext());
dealerSign.setImageResource(R.drawable.dealer);
dealerSign.setAdjustViewBounds(true);
dealerSign.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
我在setImageResource行收到错误。
04-09 22:04:37.226: E/AndroidRuntime(722): FATAL EXCEPTION: main
04-09 22:04:37.226: E/AndroidRuntime(722): java.lang.OutOfMemoryError
04-09 22:04:37.226: E/AndroidRuntime(722): at android.graphics.Bitmap.nativeCreate(Native Method)
04-09 22:04:37.226: E/AndroidRuntime(722): at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
04-09 22:04:37.226: E/AndroidRuntime(722): at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
04-09 22:04:37.226: E/AndroidRuntime(722): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
04-09 22:04:37.226: E/AndroidRuntime(722): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
04-09 22:04:37.226: E/AndroidRuntime(722): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
04-09 22:04:37.226: E/AndroidRuntime(722): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
04-09 22:04:37.226: E/AndroidRuntime(722): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:767)
04-09 22:04:37.226: E/AndroidRuntime(722): at android.content.res.Resources.loadDrawable(Resources.java:1937)
04-09 22:04:37.226: E/AndroidRuntime(722): at android.content.res.Resources.getDrawable(Resources.java:664)
04-09 22:04:37.226: E/AndroidRuntime(722): at android.widget.ImageView.resolveUri(ImageView.java:537)
04-09 22:04:37.226: E/AndroidRuntime(722): at android.widget.ImageView.setImageResource(ImageView.java:310)
04-09 22:04:37.226: E/AndroidRuntime(722): at game.spathi.GameView.init(GameView.java:261)
04-09 22:04:37.226: E/AndroidRuntime(722): at game.spathi.Game.onCreate(Game.java:92)
...
我在这里做错了什么?
谢谢!
答案 0 :(得分:3)
试一试 -
Bitmap yourBitmap;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
offerImage.compress(Bitmap.CompressFormat.PNG, 0, baos);
byte[] img = shrinkBitmap(baos.toByteArray());
//convert this byte image to bitmap and load it...
private byte[] shrinkBitmap(byte[] data){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; //try to decrease decoded image
options.inPurgeable = true; //purgeable to disk
Bitmap bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeByteArray(data, 0, data.length, options), 100, 100, true);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 20, buf);
return(buf.toByteArray());
}