我正在尝试setImageBitmap,但即使我在这里实现了https://developer.android.com/topic/performance/graphics/load-bitmap.html指南,我仍然得到以下OutOfMemoryError:
java.lang.OutOfMemoryError: Failed to allocate a 11345668 byte allocation with 1206384 free bytes and 1178KB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:478)
at com.example.myapp.ImageOptimized.decodeSampledBitmapFromResource(ImagenOptimizada.java:50)
at com.example.myapp.Game3Players.onAnimationEnd(Game3Players.java:511)
at android.view.animation.Animation$3.run(Animation.java:381)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
这是我使用该方法的代码的一部分。我随机选择一张卡片来显示一张图片,剩下的就会显示另一张图片:
ImageView[] cardsArray = new ImageView[3];
cardsArray[0]=cardOne;
cardsArray[1]=cardTwo;
cardsArray[2]=cardThree;
final int index = new Random().nextInt(cardsArray.length);
cardsArray[index].setImageBitmap(ImageOptimized.decodeSampledBitmapFromResource(getResources(), R.drawable.skull, 250, 250));
for (int i=0; i<cardsArray.length; i++){
if (cardsArray[index]!=cardsArray[i]){
cardsArray[i].setImageBitmap(ImageOptimized.decodeSampledBitmapFromResource(getResources(), R.drawable.safe, 250, 250));
}
}
我第一次尝试这个是因为,因为我正在替换另一个图像,我想使用相同的宽度和高度:
cardsArray[index].setImageBitmap(ImageOptimized.decodeSampledBitmapFromResource(getResources(), R.drawable.skull, cardsArray[index].getWidth(), cardsArray[index].getHeight()));
但这给了我一个错误。这就是为什么我决定使用固定大小250,但它一直让我错误。有趣的是,有时代码运行时没有问题,但是当操作重复时 - 可能是第三次,第四次,有时是第五次 - 它会崩溃。我可能做错了导致内存泄漏吗?
万一你也想看看我遵循指南的班级:
public class ImageOptimized {
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);
}
}
您可以使用加载和缓存图像库来获得更好的性能,例如Glide或Picasso。
例如,Glide的使用非常简单:
Glide.with(this).load(R.drawable.safe).into(imageView);
您也可以调整它们的大小:
Glide.with(this).load(R.drawable.safe).apply(new RequestOptions().override(250, 250).into(imageView);
大多数这些图书馆都自己处理他们使用的资源清理工作;这意味着他们有效地处理内存。
如果您可以选择,也尽量不要使用非常大的分辨率图像并使用PNG格式。
我几天前有同样的问题,我错过了我的drawable的复制/粘贴,而不是把它放在drawable-hdpi
(例如)我把它放在drawable
文件夹,没有扩展名,将drawable移动到一个可扩展的文件夹,扩展为我工作。
您也可以将这些行添加到manifest.xml
中
机器人:硬件加速=“假”
机器人:largeHeap = “真”