Search code examples
androiduniversal-image-loader

android universal image loader out of memory error


i use Universal image loader 1.9.5 to load image from url.
this is Adapter

public class BinderDataImg extends BaseAdapter {

static final String KEY_IMG = "img";
LayoutInflater inflater;
List<HashMap<String,String>> imgHashmap;
ViewHolder holder;

ImageLoader imageLoader = ImageLoader.getInstance();

public BinderDataImg(Activity act, List<HashMap<String,String>> map) {
    this.imgHashmap = map;
    inflater = (LayoutInflater) act
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    // TODO Auto-generated method stub
    return imgHashmap.size();
}

public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null){
        vi = inflater.inflate(R.layout.list_img, null);
        holder = new ViewHolder();
        holder.iv_img =(ImageView)vi.findViewById(R.id.imageViewImg);

        vi.setTag(holder);
    }
    else{

        holder = (ViewHolder)vi.getTag();
    }

    String uri = imgHashmap .get(position).get(KEY_IMG);

    imageLoader.displayImage(uri, holder.iv_img);

    return vi;
}

static class ViewHolder{
    ImageView iv_img;
}

}

this is Activity with config of Universal image loader

public class ActImg extends AppCompatActivity {

static final String KEY_IMG = "img";
List<HashMap<String,String>> imgHashmap;

List<ClassImg> imgList = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_act_img);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .resetViewBeforeLoading(true)
            .displayer(new FadeInBitmapDisplayer(300))
            .showImageOnLoading(R.drawable.img_loading)
            .build();

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
            .defaultDisplayImageOptions(defaultOptions)
            .threadPoolSize(3)
            .memoryCache(new WeakMemoryCache())
            .diskCacheSize(100 * 1024 * 1024)
    .build();

    Intent i = getIntent();

    CustomListView lvi = (CustomListView)findViewById(R.id.listViewImg);

    try {
        XmlPullParserImg parser = new XmlPullParserImg();
        imgList = parser.parse(getAssets().open(file));
        BinderDataImg binderdata = new BinderDataImg(this, imgHashmap);
        lvi.setAdapter(binderdata);

    }catch(IOException e) {
        e.printStackTrace();
    }

}

update : this is logcat
05-03 03:50:38.011 18383-18878/tttdigital.readmangaonline E/ImageLoader: null
java.lang.OutOfMemoryError
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:689)
at android.graphics.Bitmap.createBitmap(Bitmap.java:666)
at android.graphics.Bitmap.createBitmap(Bitmap.java:599)
at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.considerExactScaleAndOrientatiton(BaseImageDecoder.java:218)
at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.decode(BaseImageDecoder.java:91)
at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeImage(LoadAndDisplayImageTask.java:265)
at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:238)
at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:136)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)

I can load image from url , but some image can't display and I get OutOfMemory error.
how to fix it?


Solution

  • there are a couple of ways you can do this,

    1)increase your memory cache size below example sets it to 15

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).threadPoolSize(3)
                .threadPriority(Thread.MIN_PRIORITY + 3)
                .denyCacheImageMultipleSizesInMemory()
                .memoryCacheSize(1048576 * 15)               
                .discCache(new UnlimitedDiscCache(cacheDir))
               .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
                .build();
    

    2)Reduce thread pool size i have used 3 below

    ImageLoaderConfiguration.Builder(context).threadPoolSize(3)
    

    3)Also try this setting as this works in glide instead of ARGB_8888 use RGB_565

    .bitmapConfig(Bitmap.Config.RGB_565) 
    

    Also in general you can use largeHeap=true in manifest file if you are dealing with loading a large set of images in your app