Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

out of memory - Android Outofmemory drawable

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...