0

I just startet writing an app for Android. I have 3 activitys that use the same xml layout, an grid view. They also use all the same adapter to show an imageview and an textview in each grid item. When you select an object in the first activity, you get to the second and then to the third.

First I used the normal method "imageView.setImageResource(drawableid)". It wasn't really a problem, but when I put more images into the gridview, I got the outofmemory error.

So I search for a solution. I tried the thing from the android dev site with decode bitmap, resize and Async task and so on. The other thing was the picasso library, wich would be very simple with "Picasso.with(mContext).load(imageURI).into(imageView);". But picasso ist to slow for the gridview. It looks messed up while scrolling. And sometimes some images weren't load.

All metodes fill the memory and the app crashes when I open the activities a couple times. What am I doing wrong? Can I free up the memory by myself, when I leave the first activity to the second?

André
  • 91
  • 1
  • 7

3 Answers3

4

Best solution is use the RecyclerView in your app https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html

Jigsh
  • 156
  • 1
  • 8
  • 2
    This solved my problem, thanks. RecyclerView was never mentioned when I search for out of memory on image loading. – André Jul 07 '15 at 20:23
0

There was an answer in SO about freeing up memory manually. I couldn't find it now so I am pasting the answer I am implementing

Define the following function

    //To free up memory taken by adapterViews and others
private void unbindDrawables(View view) {
    if (view.getBackground() != null)
        view.getBackground().setCallback(null);

    if (view instanceof ImageView) {
        ImageView imageView = (ImageView) view;
        imageView.setImageBitmap(null);
    } else if (view instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup) view;
        for (int i = 0; i < viewGroup.getChildCount(); i++)
            unbindDrawables(viewGroup.getChildAt(i));

        if (!(view instanceof AdapterView))
            viewGroup.removeAllViews();
    }
}

then in your onDestroy method use

unbindDrawables(findViewById(R.id.view_to_unbind));
System.gc();

This has stopped my app from crashing on orientation change.

rockfight
  • 1,916
  • 2
  • 20
  • 31
0

You load image thumbnail in grid view not the main image after clicking on gridview you make a request to load the main image. Store thumbnails on server also.

Vaibhav Ajay Gupta
  • 463
  • 2
  • 4
  • 18
  • I don't want to load bigger images. The gridview is displaying some sorts of different machines. They have to be good visible from the beginning, because you make only a decision, wich machine you want. I think a tiny thumbnail isn't good for that. – André Jul 07 '15 at 06:21