0

i'm trying to create an application contains different categories and each category contains range of images (All images are downloaded from their links) and i working with Glide Library ,i did all that but have problem with memory ram my application take a lot of memory (The value varies depending on the device) reason of that is when i moving between categories The images are loaded and stored in the cache , how i can fix this issue

this one of fragments .java

public class Art extends Fragment {
GridView myGrid;
CategoryAdapter adapter;

public Art() {
    // Required empty public constructor
}

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup   container, Bundle savedInstanceState) {
    final ArrayList<String> movieList =  new ArrayList<>();
    /*Source Art*/
    movieList.add("http://i.imgur.com/INlyb2w.jpg");
    movieList.add("http://i.imgur.com/ZvvBaq8.jpg");
    /*****/

    View v =  inflater.inflate(R.layout.fragment_art, container, false);
    myGrid= (GridView) v.findViewById(R.id.gridViewCategory);
    adapter =new CategoryAdapter(getActivity(),movieList);
    myGrid.setAdapter(adapter);
    myGrid.setOnItemClickListener(new    android.widget.AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int   position, long id) {
                    Intent popup = new Intent(getActivity(), Pop.class);
                    popup.putExtra("WallpaperURL", movieList.get(position));
                    startActivity(popup);
          }
       });
      return v;
   }
}
 class CategoryAdapter extends BaseAdapter {
  private Context context;
  private ArrayList<String> imageId= new ArrayList<>();
  CategoryAdapter(Context context,ArrayList<String> imageId){
    this.context=context;
    this.imageId=imageId;
 }
 @Override
 public int getCount() {
    return imageId.size();
 }

 @Override
public Object getItem(int position) {
    return imageId.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}
private class ViewHolder{
    ImageView CategoryImage;
    ViewHolder(View view){
        CategoryImage = (ImageView) view.findViewById(R.id.cat_imageView);
    }
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row=convertView;
    ViewHolder holder;
    if(row == null){
        LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row=inflater.inflate(R.layout.picture_item,parent,false);
        holder=new ViewHolder(row);
        row.setTag(holder);
    }
    else{
        holder=(ViewHolder) row.getTag();
    }
    Glide.with(context).load(imageId.get(position)).placeholder(R.drawable.arrw).into(holder.CategoryImage);
    return row;
}

}

Zoe
  • 27,060
  • 21
  • 118
  • 148
MrMR
  • 279
  • 6
  • 16
  • 1
    do you scale down the images? using unscaled images from the web can cause application crashes due to out of memory exceptions. – Stefan May 03 '17 at 17:43
  • @Stefan please can you explain more ?? – MrMR May 03 '17 at 17:46
  • 1
    Try using the [**Static** ViewHolder method](http://stackoverflow.com/questions/33702163/why-to-use-static-with-recyclerview-viewholder) – airowe May 03 '17 at 17:53
  • 1
    first of all, I am not a java expert; but in general working with a large image set will lead to memory issues. Basically there are 2 options: 1 don't cache images in memory, which will decrease your application performance. The second, 2) one typical thing when working with images in lists, or in general, is that, especially large downloaded images are bigger than the screen resolution. It's best to scale these down to the resolution you want them to display (like thumbs for example). – Stefan May 03 '17 at 17:54
  • 1
    how much is 'a lot'? – Shmuel May 03 '17 at 18:03
  • 1
    although, your issue seems to be more specific :| – Stefan May 03 '17 at 18:04
  • @Shmuel in (Galaxy S4 /Android 5.0.1 ) take 202mb – MrMR May 03 '17 at 18:10
  • how i can clear cache after reopen app ?? please help me – MrMR May 03 '17 at 18:24

0 Answers0