java.lang.OutOfMemoryError:无法分配 571401228 字节分配,其中 16777216 个可用字节和 495MB 直到 OOM
我的 android 应用程序出错.我想使用 viewPager 作为 android 图像滑块,为此我得到了我的 AndroidImageAdapter.java,但是当涉及到方法 instantiateItem 时,应用程序中止,我不知道如何修复它.我已经查找了失败并添加了
I got an error with my android application. I want to use a viewPager as an android image slider, for this I got my AndroidImageAdapter.java, but when it comes to the method instantiateItem the application aborts and I don't know how to fix it. I already looked up the failure and added
android:hardwareAccelerated="false"
android:largeHeap="true"
到我的清单,但它仍然存在.这是完整的错误代码:
to my manifest, but it still remains. Here is the complete error code:
java.lang.OutOfMemoryError: Failed to allocate a 571401228 byte allocation with 16777216 free bytes and 495MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:2228)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:4211)
at android.content.res.Resources.loadDrawable(Resources.java:4085)
at android.content.res.Resources.getDrawable(Resources.java:2005)
at android.content.res.Resources.getDrawable(Resources.java:1987)
at android.content.Context.getDrawable(Context.java:464)
at android.widget.ImageView.resolveUri(ImageView.java:827)
at android.widget.ImageView.setImageResource(ImageView.java:434)
at bubusapp.anni.privateapplication.com.schnurr.asam.robin.bubusapp.AndroidImageAdapter.instantiateItem(AndroidImageAdapter.java:50)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1002)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1150)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1084)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
at android.view.View.measure(View.java:20151)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:400)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3158)
at android.view.View.measure(View.java:20151)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2594)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1549)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1841)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
at android.view.Choreographer.doCallbacks(Choreographer.java:695)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(Zy
还有我的java代码:
And my java code:
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class AndroidImageAdapter extends PagerAdapter{
private Context mContext;
private int[] images;
AndroidImageAdapter(Context context, int remainingDays) {
this.mContext = context;
images = loadImages(remainingDays);
}
@Override
public int getCount() {
return images.length;
}
@Override
public boolean isViewFromObject(View v, Object obj) {
return v == ((ImageView) obj);
}
@Override
public Object instantiateItem(ViewGroup container, int i) {
ImageView mImageView = new ImageView(mContext);
mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
try
{
mImageView.setImageResource(images[i]);//here comes the exception - outofmemory
}
catch(Exception ex)
{
Log.i("imageResException", ex.toString());
}
((ViewPager) container).addView(mImageView, 0);
return mImageView;
}
@Override
public void destroyItem(ViewGroup container, int i, Object obj) {
((ViewPager) container).removeView((ImageView) obj);
}
private int[] loadImages(int remainingDays)
{
int[] allImages = new int[] {R.drawable.pos1_1, R.drawable.pos1_2, R.drawable.pos1_3, R.drawable.pos1_4, R.drawable.pos1_5, R.drawable.pos1_6, R.drawable.pos1_7, R.drawable.pos1_8, R.drawable.pos1_9, R.drawable.pos1_10, R.drawable.pos1_11,
R.drawable.pos1_12, R.drawable.pos1_13, R.drawable.pos1_14, R.drawable.pos1_15, R.drawable.pos1_16, R.drawable.pos1_17, R.drawable.pos1_18, R.drawable.pos1_19,R.drawable.pos1_20, R.drawable.pos1_21, R.drawable.pos1_22, R.drawable.pos1_23, R.drawable.pos1_24, R.drawable.pos1_25, R.drawable.pos1_26, R.drawable.pos1_27,
R.drawable.pos1_28,
R.drawable.pos1_29,
R.drawable.pos1_30,
R.drawable.pos1_31,
R.drawable.pos1_32,
R.drawable.pos1_33,
R.drawable.pos1_34,
R.drawable.pos1_35,
R.drawable.pos1_36,
R.drawable.pos1_37,
R.drawable.pos1_38,
R.drawable.pos1_39,
R.drawable.pos1_40,
R.drawable.pos1_41,
R.drawable.pos1_42,
R.drawable.pos1_43,
R.drawable.pos1_44,
R.drawable.pos1_45,
R.drawable.pos1_46,
R.drawable.pos1_47,
R.drawable.pos1_48,
R.drawable.pos1_49,
R.drawable.pos1_50,
R.drawable.pos1_51,
R.drawable.pos1_52,
R.drawable.pos1_53,
R.drawable.pos1_54,
R.drawable.pos1_55,
R.drawable.pos1_56,
R.drawable.pos1_57,
R.drawable.pos1_58,
R.drawable.pos1_59,
R.drawable.pos1_60,
R.drawable.pos1_61,
R.drawable.pos1_62,
R.drawable.pos1_63,
R.drawable.pos1_64,
R.drawable.pos1_65,
R.drawable.pos1_66,
R.drawable.pos1_67,
R.drawable.pos1_68,
R.drawable.pos1_69,
R.drawable.pos1_70,
R.drawable.pos1_71,
R.drawable.pos1_72,
R.drawable.pos1_73,
R.drawable.pos1_74,
R.drawable.pos1_75,
R.drawable.pos1_76,
R.drawable.pos1_77,
R.drawable.pos1_78,
R.drawable.pos1_79,
R.drawable.pos1_80,
R.drawable.pos1_81,
R.drawable.pos1_82,
R.drawable.pos1_83,
R.drawable.pos1_84,
R.drawable.pos1_85,
R.drawable.pos1_86,
R.drawable.pos1_87,
R.drawable.pos1_88,
R.drawable.pos1_89,
R.drawable.pos1_90,
R.drawable.pos1_91,
R.drawable.pos1_92,
R.drawable.pos1_93,
R.drawable.pos1_94,
R.drawable.pos1_95,
R.drawable.pos1_96,
R.drawable.pos1_97,
R.drawable.pos1_98,
R.drawable.pos1_99,
R.drawable.pos1_100,
R.drawable.pos1_101,
R.drawable.pos1_102,
R.drawable.pos1_103,
R.drawable.pos1_104,
R.drawable.pos1_105,
R.drawable.pos1_106,
R.drawable.pos1_107,
R.drawable.pos1_108,
R.drawable.pos1_109,
R.drawable.pos1_110,
R.drawable.pos1_111,
R.drawable.pos1_112,
R.drawable.pos1_113,
R.drawable.pos1_114,
R.drawable.pos1_115,
R.drawable.pos1_116,
R.drawable.pos1_117,
R.drawable.pos1_118,
R.drawable.pos1_119,
R.drawable.pos1_120,
R.drawable.pos1_121,
R.drawable.pos1_122,
R.drawable.pos1_123,
R.drawable.pos1_124,
R.drawable.pos1_125,
R.drawable.pos1_126
};
int[] actualImages = new int[127 - remainingDays];
try
{
System.arraycopy(allImages, 0, actualImages, 0, 127 - remainingDays);
}
catch(Exception ex)
{
Log.i("ArrayCopyException: ", ex.getMessage() + "" );
}
return actualImages;
}
}
推荐答案
571401228 字节相当于 11952 x 11952 像素图像.这太大了.
571401228 bytes is the equivalent of 11952 x 11952 pixel image. This is much too large.
问题很可能有两个方面:
Most likely, the problem is two-fold:
drawable 资源本身的分辨率太大
The drawable resource's resolution is too large in its own right
您将图像放在 res/drawable/
中,因此它会根据您的实际设备密度进行放大,因此它比您开始时大 4 倍或 8 倍p>
You put the image in res/drawable/
, and so it is being upscaled based on your actual device density, so it is 4x or 8x larger than what you started with
不要将位图放在 res/drawable/
中,它是 res/drawable-mdpi/
的同义词.为各种密度创建与密度相关的绘图版本,或者将此绘图放在 res/drawable-anydpi/
中.另外,请确保分辨率合理.
Do not put bitmaps in res/drawable/
, which is a synonym for res/drawable-mdpi/
. Either create density-dependent editions of the drawables for various densities, or put this drawable in res/drawable-anydpi/
. Also, make sure that the resolution is reasonable.
相关文章