Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android gridview crashes on Galaxy 3

Ok, I think I have a real question for a change. I implemented a gridView in Android, following step by step the instructions in the Android Developers page, http://developer.android.com/resources/tutorials/views/hello-gridview.html I changed it so that when clicking on a view, the bitmap will be returned. This all works great on advanced phones like Galxy Note and Galaxy S2, and on less advanced ones like Galaxy ace and even some crappy htc from 2 years ago. But for some reason, it crashes on galaxy 3 with ICS due to OutOfMemory issues. When using about half of the images on the gridview, it does work (albeit a bit slowly). Does this make any sense to anyone?

This is my implementation of ImageAdapter:

public class ImageAdapter extends BaseAdapter {
private Context mContext;
private int mWidth;
private int mHeight;

public ImageAdapter(Context c, int width, int height) {
    mContext = c;
    mWidth = width;
    mHeight = height;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return mThumbIds[position];
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(mWidth, mHeight));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.third_world , R.drawable.annoyinggirl,
        R.drawable.asianfather, R.drawable.awkawespeng,
        R.drawable.awkpeng, R.drawable.blankdad,
        R.drawable.collegesenior, R.drawable.courage,
        R.drawable.frog, R.drawable.frynotsure,
        R.drawable.goodguygreg, R.drawable.scumbag,
        R.drawable.raptor,R.drawable.keano,
        R.drawable.successkid, R.drawable.wonka,
        R.drawable.braceyourselves, R.drawable.onedoesnotsimply,
        R.drawable.firstworldproblem, R.drawable.amitheonlyone,
        R.drawable.badluckbrian, R.drawable.interestingman,
        R.drawable.toodamnhigh, R.drawable.def    
};

}

This is the calling function from my main:

  private void changeToTemplateView(){
    setContentView(R.layout.templates);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(context, (int)(dstWidth * 1.4), (int)(dstHeight * 1.4)));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            Options opts = new Options();
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), (int) parent.getItemIdAtPosition(position), opts);

            //do stuff with bitmap          
        }
    });
}

And this is the xml for the gridview:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gridview"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"

/>

If anyone has any idea what I might be doing wrong here, I will be forever grateful

EDIT: Before the crash, I get an error in the log: "FimgApiStretch:stretch failed". Also, the scrolling in the gridview doesn't work. This is regardless of the fact that even if there aren't enough thumbnails to cause a scroll, the app will still crash

like image 337
n00b programmer Avatar asked Jun 12 '12 14:06

n00b programmer


2 Answers

try to replace fill_parent with match_parent because according to my information the attribute value fill_parent is deprecated on android 2.2 and above hope this would help

You can see this link http://developer.android.com/training/displaying-bitmaps/load-bitmap.html I hope this will help you

like image 152
Muhannad A.Alhariri Avatar answered Oct 05 '22 16:10

Muhannad A.Alhariri


AS for the question "why the hell is it crashing on galaxy 3 and not on inferior phones", the answer is "I have no clue". But, turns out I have been misusing the gridview. It is a bad idea to use the same image both for the thumbnail and for the whole image. What I did (which might be nothing more than a workaround, but it works) is to use 2 sets of images, small one and full size ines. This is done like this

For setting the thumbnails:

private Integer[] mThumbIds = {
        R.drawable.third_world__small , R.drawable.annoyinggirl__small,
        R.drawable.asianfather__small, R.drawable.awkawespeng__small,
        R.drawable.awkpeng__small, R.drawable.blankdad__small,
        R.drawable.collegesenior__small, R.drawable.courage__small,
        R.drawable.frog__small, R.drawable.frynotsure__small,
        R.drawable.goodguygreg__small, R.drawable.scumbag__small,
        R.drawable.raptor__small,R.drawable.keano__small,
        R.drawable.successkid__small, R.drawable.wonka__small,
        R.drawable.braceyourselves__small, R.drawable.onedoesnotsimply__small,
        R.drawable.firstworldproblem__small, R.drawable.amitheonlyone__small,
        R.drawable.badluckbrian__small, R.drawable.interestingman__small,
        R.drawable.toodamnhigh__small, R.drawable.def__small    
};

public View getView(int position, View convertView, ViewGroup parent) {
    //clearAllResources();
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(mWidth, mHeight));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

Then, create another array:

private Map<Integer, Integer> mThumbIdToFullSizeId = new HashMap<Integer,Integer>();
public ImageAdapter(Context c, int width, int height) {
    mContext = c;
    mWidth = width;
    mHeight = height;
    mThumbIdToFullSizeId.put(R.drawable.third_world__small, R.drawable.third_world);
    mThumbIdToFullSizeId.put(R.drawable.annoyinggirl__small,R.drawable.annoyinggirl);
    mThumbIdToFullSizeId.put(R.drawable.asianfather__small, R.drawable.asianfather);
    mThumbIdToFullSizeId.put(R.drawable.awkawespeng__small, R.drawable.awkawespeng);
    mThumbIdToFullSizeId.put(R.drawable.awkpeng__small, R.drawable.awkpeng);
    mThumbIdToFullSizeId.put(R.drawable.blankdad__small, R.drawable.blankdad);
    mThumbIdToFullSizeId.put(R.drawable.collegesenior__small, R.drawable.collegesenior);
    mThumbIdToFullSizeId.put(R.drawable.courage__small, R.drawable.courage);
    mThumbIdToFullSizeId.put(R.drawable.frog__small, R.drawable.frog);
    mThumbIdToFullSizeId.put(R.drawable.frynotsure__small, R.drawable.frynotsure);
    mThumbIdToFullSizeId.put(R.drawable.goodguygreg__small, R.drawable.goodguygreg);
    mThumbIdToFullSizeId.put(R.drawable.scumbag__small, R.drawable.scumbag);
    mThumbIdToFullSizeId.put(R.drawable.raptor__small, R.drawable.raptor);
    mThumbIdToFullSizeId.put(R.drawable.keano__small, R.drawable.keano);
    mThumbIdToFullSizeId.put(R.drawable.successkid__small, R.drawable.successkid);
    mThumbIdToFullSizeId.put(R.drawable.wonka__small, R.drawable.wonka);
    mThumbIdToFullSizeId.put(R.drawable.braceyourselves__small, R.drawable.braceyourselves);
    mThumbIdToFullSizeId.put(R.drawable.onedoesnotsimply__small, R.drawable.onedoesnotsimply);
    mThumbIdToFullSizeId.put(R.drawable.firstworldproblem__small, R.drawable.firstworldproblem);
    mThumbIdToFullSizeId.put(R.drawable.amitheonlyone__small, R.drawable.amitheonlyone);
    mThumbIdToFullSizeId.put(R.drawable.badluckbrian__small, R.drawable.badluckbrian);
    mThumbIdToFullSizeId.put(R.drawable.interestingman__small, R.drawable.interestingman);
    mThumbIdToFullSizeId.put(R.drawable.toodamnhigh__small, R.drawable.toodamnhigh);
    mThumbIdToFullSizeId.put(R.drawable.def__small, R.drawable.def);
}

Then, for the getview function:

public long getItemId(int position) {

    return mThumbIdToFullSizeId.get(mThumbIds[position]);
}

And everything will be much more memory friendly. I'm giving the checkmark to Muhannad for giving me the link to http://developer.android.com/training/displaying-bitmaps/load-bitmap.html which helped alot

like image 23
n00b programmer Avatar answered Oct 05 '22 17:10

n00b programmer