the png loading get out of memory android

I have a fragment that have a grid view.I'm putting 70 images in grid view.but when i run the app it get out of memory.how o solve it?should i convert all images to bitmap? this is my fragment class:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_games, container, false); mDrawerList = (GridView) rootView.findViewById(R.id.list_slidermenu); icons=new int[50]; for(int i=1 ;i<icons.length ;i++) { icon_id="@drawable/data_" + i; icons[i]=getResources().getIdentifier(icon_id, null, MainActivity.PACKAGE_NAME); Log.e("log",""+icons[i]); } navDrawerItems = new ArrayList<NavDrawerItem>(); for(int i=0 ;i<icons.length ;i++) { navDrawerItems.add(new NavDrawerItem(icons[i])); } adapter = new NavDrawerListAdapter(getActivity(), navDrawerItems); mDrawerList.setAdapter(adapter); return rootView; }

and this is my adapter class:

public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); convertView = mInflater.inflate(R.layout.itemlist, null); } ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon); imgIcon.setImageResource(navDrawerItems.get(position).getIcon()); // displaying count // check whether it set visible or not return convertView; }

--------------Solutions-------------

You have to consider about several facts here .

  1. If 70 images gave you a OOM exception, that means your images are must larger in size. But most of the time, you do not need the image as it is. Your image is 1024 * 1024 pixels, but you show it within 100 * 100 scale image view. In such cases you have to find the required size and scale down the image where it doesnt waste heap and also image doesnt get pixalated. read here
  2. You have to use some kind of loading technique such as progressing loading instead of loading all 70 at once into the grid. Then images will be load when only user scroll the grid. It will not waste the memory.
  3. You have de-allocate all the images which are not currently shown on the grid for a given particular moment.
  4. You have to set the large heap for the app though it is not recommended but have to use in these kind of apps.

As Android strictly just allows you to allocate a specific amount of memory from SDK you have to use lower quality pictures or not as many pictures (70 is a high value). Loading them into bitmaps will increase your need in memory or at least stay the same. If you really want to load 70 pictures as big as they are at the time you have to consider using the NDK (But that's not easy and here I think too much amount of work).

I cannot understand how you can runn out of memory when your bitmap's sum is 1MB. But there is a easy way to overcome the Garbage Collector when you need more memory for Bitmaps. (I`ve found this Solution yesterday). Convert each Bitmap into an ByteBuffer you allocate from SDK. This ByteBuffer doesn't count for your App's Memory. The Code may look like this:



ByteBuffer mByteBuffer= ByteBuffer.allocateDirect(w*h*2); //w and h stands for width and height of your Bitmap (Format: RGB_565)

mByteBuffer.order(ByteBuffer.nativeOrder);

mBitmap.copyPixelsToBuffer(mByteBuffer); //Now your Bitmap is stored in an Buffer out of Java,and we can recycle the Bitmap. When you want to Display this ByteBuffer you can write

mByteBuffer.position(0);

mOtherBitmap.copyPixelsFromBuffer(mByteBuffer) and your Bitmap is ready to go. It's really fast in my Case (~5ms)

But I reccomend this methode only when your Images sum are bigger than 1MB

Category:android Time:2017-12-06 Views:1

Related post

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.977 (s). 13 q(s)