Android Outofmemory Conclusion

this is my drawable in array for my viewpager, when I run it, I get an error from memory, is there any way to reduce the image size, etc.? I am looking for a lot, but I can not use them. please, help...

GalImages = new int[] { R.drawable.tutorials_01_android_en,R.drawable.tutorials_02_android_en,R.drawable.tutorials_03_android_en,R.drawable.tutorials_04};break @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView = new ImageView(context); final int temp = position; imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); imageView.setImageResource(GalImages[position]); ((ViewPager) container).addView(imageView, 0); imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) {} }); return imageView; } 
+7
android out-of-memory
source share
5 answers

Its a known bug , its not due to large files. Since Android caches Drawables, it gets out of memory after using multiple images. But I found an alternative way to do this, skipping the default caching system for Android.

Soultion : Create a dedicated folder in Assets and move the images to the drawable folder in the assets and use the following function to get BitmapDrawable

 public static Drawable getAssetImage(Context context, String filename) throws IOException { AssetManager assets = context.getResources().getAssets(); InputStream buffer = new BufferedInputStream((assets.open("drawable/" + filename + ".png"))); Bitmap bitmap = BitmapFactory.decodeStream(buffer); return new BitmapDrawable(context.getResources(), bitmap); } 

Disclaimer: https://stackoverflow.com/posts/6116316/revisions

Also add the line below to the manifest file

 android:largeHeap="true" 
+16
source share

Try moving your drawings from lower MPI to HDPI or to larger folders such as XHDPI, depending on your resolution.

The sizes of my images ranged from 400-800 pixels. I saved my drawings in MDPI, which threw OutOfMemory on my Galaxy S4. I moved them to HDPI and he solved the problem.

+11
source share

As mentioned by Muhammad, try moving the images in drawable (or MDPI) to another measurement folder in HDPI or higher format.

Always try to use compressed PNGs as much as possible. There are many online sites that you can compress them on a very large scale.

link: http://developer.android.com/guide/practices/screens_support.html

+2
source share

As long as your images are not very large, you can use the drawable-nodpi folder for your PNGs. I can confirm that this solved the problem for me.

+2
source share

Add android:largeHeap:"true" to your manifest file and move your drawings to the mipmap-hopi .

[android] [java] [android-studio]

0
source share

All Articles