我正在添加一些资源 onCreate,它在 Android 2.2 到 2.3.x 上运行良好,但在 4.0+ 上崩溃是代码片段
我已将所有资源都放在 drawable-mdpi 中,这可能是问题吗?
private List<Drawable> drawables;
private void getDrawablesList() {
drawables = new ArrayList<Drawable>();
for(int i=0; i<26; i++)
drawables.add(getResources().getDrawable(Utility.getImageIDofGalleryAlphabets(i)));
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
getDrawablesList();
setupUI();
}
我在 getDrawablesList 以下行中遇到异常
drawables.add(getResources().getDrawable(Utility.getImageIDofGalleryAlphabets(i)));
以下是崩溃日志:
FATAL EXCEPTION: main
java.lang.OutOfMemoryError
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
at android.content.res.Resources.loadDrawable(Resources.java:1937)
at android.content.res.Resources.getDrawable(Resources.java:664)
at com.rdx.gallery.GalleryDemoActivity.getDrawablesList(GalleryDemoActivity.java:180)
at com.rdx.gallery.GalleryDemoActivity.onCreate(GalleryDemoActivity.java:65)
at android.app.Activity.performCreate(Activity.java:4635)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2031)
at android.app.ActivityThread.access$600(ActivityThread.java:126)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1166)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4486)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
04-01 08:31:28.894: I/Process(11767): Sending signal. PID: 11767 SIG: 9
04-01 08:31:28.894: E/AndroidRuntime(11767): Handle UnCaght exceptions. KILLING PID: 11767
我知道我开始加载 26 个资源。26 张图片的总大小 = 2.57 MB,我认为这不是太大..
出了什么问题?请分享你的看法