8

this is my drawable in array for my viewpager, when i run it, i results in out of memory error, is there any way to reduce the image size or etc? i do lots of search but i can't use them as well.. 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;
}
Kennett
  • 467
  • 2
  • 7
  • 17

6 Answers6

19

Its a known bug, its not because of large files. Since Android Caches the Drawables, its going out of memory after using few images. But i found alternate way for it, by skipping the android default cache system.

Soultion: Create a drawable folder in Assets and move the images to "drawable" folder in 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);
}

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

Also that add the line below in your manifest file

android:largeHeap="true"
Community
  • 1
  • 1
Quamber Ali
  • 2,170
  • 25
  • 46
12

Try moving your drawables from lower MPI to HDPI or to even higher dimension folders like XHDPI depending on your resolution.

My images's dimensions ranged from 400-800 pixels. I stored my drawables in MDPI that was throwing OutOfMemory on my Galaxy S4. I moved them to HDPI and it solved the problem.

Muhammad
  • 425
  • 1
  • 6
  • 14
  • 1
    I almost can't believe that something as simple as this solved a memory problem I was having immediately. Thank you! – Benjamin S Mar 02 '16 at 18:21
2

As mentioned by Muhammad, try moving your images in drawable(or MDPI) to a different dimension folder as HDPI or higher.

Always try to use compressed PNGs as far as possible. There are a lot of online sites that you can compress them in a very large scale.

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

Poornamith
  • 184
  • 3
  • 7
2

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

Suleiman19
  • 742
  • 1
  • 7
  • 21
0

Add android:largeHeap:"true" to your manifest file and move your drawable images into the folder mipmap-hopi.

[android] [java] [android-studio]

0

has @Muhammad mentioned here before, creating multiple images included (hdpi, ldpi,mdpi,xdpi etc) solve the problem for me.

because it took me some time figure how to add those files here are some print screens that explain it: Drawable -->New --> Batch Drawable Import (for multiply files) enter image description here
then Add the files and then chose the resolutions enter image description here

Shai Epstein
  • 179
  • 1
  • 3