Android的内存不足绘制 [英] Android Outofmemory drawable

查看:142
本文介绍了Android的内存不足绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的阵列我viewpager绘制,当我运行它,我会导致内存不足的错误,有没有什么办法来缩小图像尺寸或等? 我做很多的搜索,但我不能使用它们以及.. 请帮助...

  GalImages =新INT [] { R.drawable.tutorials_01_android_en,R.drawable.tutorials_02_android_en,R.drawable.tutorials_03_android_en,R.drawable.tutorials_04};break

@覆盖
公共对象instantiateItem(ViewGroup中的容器,INT位置){

    ImageView的ImageView的=新ImageView的(上下文);
    最终诠释临时=位置;
    imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    imageView.setImageResource(GalImages [位置]);

    ((ViewPager)容器).addView(ImageView的,0);
    imageView.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(查看为arg0){}
    });
    返回ImageView的;
}
 

解决方案

它是一个因为大文件的已知的bug ,它不是。由于Android的高速缓存可绘,其使用几张图片后,会出现内存不足。但我发现另一个方法是,通过跳过了Android默认的缓存系统。

Soultion : 创建于资产绘制文件夹,然后将图像绘制文件夹中的资产,​​并使用以下函数来获取BitmapDrawable

 公共静态绘制对象getAssetImage(上下文的背景下,字符串文件名)抛出IOException异常{
    AssetManager资产= context.getResources()getAssets()。
    InputStream的缓冲区=新的BufferedInputStream((assets.open(绘制/+文件名+png格式)));
    点阵位图= BitmapFactory.de codeStream(缓冲区);
    返回新BitmapDrawable(context.getResources(),位图);
}
 

Refrence:<一href="http://stackoverflow.com/posts/6116316/revisions">http://stackoverflow.com/posts/6116316/revisions

另外的在清单文件中添加以下

 安卓largeHeap =真
 

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;
}

解决方案

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 : http://stackoverflow.com/posts/6116316/revisions

Also that add the line below in your manifest file

android:largeHeap="true"

这篇关于Android的内存不足绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆