ImageSwitcher and memory errors

I use very simple code to show a gallery of images stored as drawings. All images are less than 200kb in size and have a resolution of 1024x576 pixels.

my code works fine on the 4 devices I tested, and for most of my users. But some of them continue to get a known issue with vm budget memory.

he is my code:

in the onCreate () method of my activity:

// set Factory for Image Switcher imageSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher); imageSwitcher.setFactory(new MyImageSwitcherFactory()); 

Factory class:

 private class MyImageSwitcherFactory implements ViewFactory { public View makeView() { ImageView imageView = new ImageView(Main.this); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); return imageView; } } 

and code for displaying images:

 try { // this is internal image loaded from drawables imageSwitcher.setImageResource(images.get(imageNumber - 1).imageResourceID); } catch (Exception e) { // no image will be shown } 

The error on some phones (a very small percentage of downloads) in the try / catch block and with some phone actions cannot start at all.

I read about recycling bitmaps and memory leaks, but my activity runs in sigle-top mode and does not recreate when orientation changes, so this is always the same action. I also do not use bitmap images directly.

Can I improve my code to get rid of this error?

first type of error:

 java.lang.OutOfMemoryError: bitmap size exceeds VM budget at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:464) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:340) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697) at android.content.res.Resources.loadDrawable(Resources.java:1705) at android.content.res.Resources.getDrawable(Resources.java:580) at android.widget.ImageView.resolveUri(ImageView.java:548) at android.widget.ImageView.setImageResource(ImageView.java:270) at android.widget.ImageSwitcher.setImageResource(ImageSwitcher.java:41) at com.mobilebabytoys.animalsoundsfree.Main.showNextImage(Main.java:356) at com.mobilebabytoys.animalsoundsfree.Main.access$0(Main.java:284) at com.mobilebabytoys.animalsoundsfree.Main$MyGestureDetector.onFling(Main.java:861) at android.view.GestureDetector.onTouchEvent(GestureDetector.java:517) at com.mobilebabytoys.animalsoundsfree.Main$8.onTouch(Main.java:218) at android.view.View.dispatchTouchEvent(View.java:3705) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:874) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1695) at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116) at android.app.Activity.dispatchTouchEvent(Activity.java:2068) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1679) at android.view.ViewRoot.handleMessage(ViewRoot.java:1697) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4568) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) at dalvik.system.NativeStart.main(Native Method) 

second type of error:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobilebabytoys.animalsoundsfree/com.mobilebabytoys.animalsoundsfree.Main}: android.view.InflateException: Binary XML file line #15: Error inflating class <unknown> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3812) at android.app.ActivityThread.access$2300(ActivityThread.java:126) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1936) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4595) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) at dalvik.system.NativeStart.main(Native Method) Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class <unknown> at android.view.LayoutInflater.createView(LayoutInflater.java:513) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563) at android.view.LayoutInflater.rInflate(LayoutInflater.java:618) at android.view.LayoutInflater.inflate(LayoutInflater.java:407) 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:207) at android.app.Activity.setContentView(Activity.java:1629) at com.mobilebabytoys.animalsoundsfree.Main.onCreate(Main.java:154) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544) ... 12 more Caused by: java.lang.reflect.InvocationTargetException at android.widget.ImageButton.<init>(ImageButton.java:78) at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:446) at android.view.LayoutInflater.createView(LayoutInflater.java:500) ... 23 more Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:464) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:340) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697) at android.content.res.Resources.loadDrawable(Resources.java:1705) at android.content.res.Resources.getDrawable(Resources.java:580) at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:160) at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788) at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729) at android.content.res.Resources.loadDrawable(Resources.java:1690) at android.content.res.TypedArray.getDrawable(TypedArray.java:548) at android.widget.ImageView.<init>(ImageView.java:115) at android.widget.ImageButton.<init>(ImageButton.java:82) ... 27 more android.view.InflateException: Binary XML file line #15: Error inflating class <unknown> at android.view.LayoutInflater.createView(LayoutInflater.java:513) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563) at android.view.LayoutInflater.rInflate(LayoutInflater.java:618) at android.view.LayoutInflater.inflate(LayoutInflater.java:407) 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:207) at android.app.Activity.setContentView(Activity.java:1629) at com.mobilebabytoys.animalsoundsfree.Main.onCreate(Main.java:154) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3812) at android.app.ActivityThread.access$2300(ActivityThread.java:126) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1936) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4595) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.reflect.InvocationTargetException at android.widget.ImageButton.<init>(ImageButton.java:78) at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:446) at android.view.LayoutInflater.createView(LayoutInflater.java:500) ... 23 more Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:464) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:340) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697) at android.content.res.Resources.loadDrawable(Resources.java:1705) at android.content.res.Resources.getDrawable(Resources.java:580) at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:160) at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788) at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729) at android.content.res.Resources.loadDrawable(Resources.java:1690) at android.content.res.TypedArray.getDrawable(TypedArray.java:548) at android.widget.ImageView.<init>(ImageView.java:115) at android.widget.ImageButton.<init>(ImageButton.java:82) ... 27 more java.lang.reflect.InvocationTargetException at android.widget.ImageButton.<init>(ImageButton.java:78) at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:446) at android.view.LayoutInflater.createView(LayoutInflater.java:500) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563) at android.view.LayoutInflater.rInflate(LayoutInflater.java:618) at android.view.LayoutInflater.inflate(LayoutInflater.java:407) 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:207) at android.app.Activity.setContentView(Activity.java:1629) at com.mobilebabytoys.animalsoundsfree.Main.onCreate(Main.java:154) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3812) at android.app.ActivityThread.access$2300(ActivityThread.java:126) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1936) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4595) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:464) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:340) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697) at android.content.res.Resources.loadDrawable(Resources.java:1705) at android.content.res.Resources.getDrawable(Resources.java:580) at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:160) at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788) at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729) at android.content.res.Resources.loadDrawable(Resources.java:1690) at android.content.res.TypedArray.getDrawable(TypedArray.java:548) at android.widget.ImageView.<init>(ImageView.java:115) at android.widget.ImageButton.<init>(ImageButton.java:82) ... 27 more java.lang.OutOfMemoryError: bitmap size exceeds VM budget at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:464) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:340) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697) at android.content.res.Resources.loadDrawable(Resources.java:1705) at android.content.res.Resources.getDrawable(Resources.java:580) at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:160) at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788) at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729) at android.content.res.Resources.loadDrawable(Resources.java:1690) at android.content.res.TypedArray.getDrawable(TypedArray.java:548) at android.widget.ImageView.<init>(ImageView.java:115) at android.widget.ImageButton.<init>(ImageButton.java:82) at android.widget.ImageButton.<init>(ImageButton.java:78) at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:446) at android.view.LayoutInflater.createView(LayoutInflater.java:500) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563) at android.view.LayoutInflater.rInflate(LayoutInflater.java:618) at android.view.LayoutInflater.inflate(LayoutInflater.java:407) 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:207) at android.app.Activity.setContentView(Activity.java:1629) at com.mobilebabytoys.animalsoundsfree.Main.onCreate(Main.java:154) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3812) at android.app.ActivityThread.access$2300(ActivityThread.java:126) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1936) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4595) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) at dalvik.system.NativeStart.main(Native Method) 

my activity layout:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/MainLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:keepScreenOn="true"> <ImageSwitcher android:keepScreenOn="true" android:id="@+id/ImageSwitcher" android:layout_width="fill_parent" android:layout_height="fill_parent"> </ImageSwitcher> </RelativeLayout> 
+6
android memory image
source share
1 answer

Bitmap Error:
You should consider sampling your bitmap and resizing it before creating it in memory. take a look at the next stream Processing large raster images

The second mistake is that the runtime cannot create a hierarchy of views from an XML layout. Do you use a custom view in your layout?

Edit : your XML format looks correct, and now that you have placed the entire dump, the only problem for me is the size of the bitmap. I still claim that you will have to dump sample images.

What is the format of your images; An image with a resolution of 1024 x 576 may be too large when decompressed in Bitmap .

See if any of these discussions provide any pointers: 1 , 2 , 3 .

+5
source share

All Articles