Monday, February 24, 2014

GridView can not display images, an error: outofMemory


This error always ran to:



This is my xml code
 xmlns: tools = "http://schemas.android.com/tools" 
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: paddingBottom = "@ dimen / activity_vertical_margin"
android: paddingLeft = "@ dimen / activity_horizontal_margin"
android: paddingRight = "@ dimen / activity_horizontal_margin"
android: paddingTop = "@ dimen / activity_vertical_margin"
tools: context = ". MainActivity">

android: id = "@ + id / gridview"
android: layout_width = "fill_parent"
android: layout_height = "fill_parent"
android: numColumns = "3">




This is the code
my java source files
 package com.siyehuazhilian.gridviewdemo; 

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class MainActivity extends Activity {
/ / Declare a control object
private GridView gridView = null;

@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
/ / To get the corresponding controls
by idgridView = (GridView) findViewById (R.id.gridview);

/ / Set the adapter for the control
gridView.setAdapter (new ImageAdapter (this));

/ / Bind the GridView listener
gridView.setOnItemClickListener (new OnItemClickListener () {

@ Override
public void onItemClick (AdapterView arg0, View arg1, int arg2,
long arg3) {
System.out.println ("sub-element was clicked");
}
});

}

@ Override
public boolean onCreateOptionsMenu (Menu menu) {
getMenuInflater () inflate (R.menu.main, menu);.
return true;
}

}


Which custom adapter code below:
 package com.siyehuazhilian.gridviewdemo; 

import java.lang.reflect.Field;
import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
/ / Resource id array
private ArrayList imgIds = new ArrayList ();
/ / Context object
private Context context;

public ImageAdapter (Context context) {
this.context = context;
/ / Get resources through reflection and the id is stored in the id array
Field [] fields = R.drawable.class.getDeclaredFields ();
for (int i = 0; i / / Only the file name contains the img will be read
if (fields [i]. getName (). contains ("img")) {
try {
imgIds.add (fields [i] getInt (R.drawable.class).);
} Catch (IllegalArgumentException e) {
e.printStackTrace ();
} Catch (IllegalAccessException e) {
e.printStackTrace ();
}
}
}
}

@ Override
public int getCount () {
return imgIds.size ();
}

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

@ Override
public long getItemId (int position) {
return position;
}

@ Override
public View getView (int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView (context);
imageView.setLayoutParams (new GridView.LayoutParams (85, 85));
imageView.setScaleType (ImageView.ScaleType.FIT_CENTER);
} Else {
imageView = (ImageView) convertView;
}

imageView.setImageResource (imgIds.get (position));
return imageView;
}

}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Come on people ah ah ah
Reply:
Memory overflow remember to recycle your image resources
Reply:
reference to the second floor wjw2066616 reply:
memory overflow remember to recycle your image resources


Novices do not know how the recovery image resources
Reply:
Picture too big, do not use first recovered bitmap, followed by the oversized picture quality reduced. With BitmapFactory.Options relevant parameters.
Reply:
references, 4th Floor birdsaction reply:
image is too large, and the first not to use recycled bitmap, followed by the oversized picture quality reduced. With BitmapFactory.Options relevant parameters.

Pictures Gaixiao no problem, that is to say the picture above recycling understand, Baidu got no clue
Reply:
Operating pictures, memory overflow.
Reply:
OOM issue has been discussed on many people recommend you look at this blog
http://blog.csdn.net/guolin_blog/article/details/9316683
Reply:
references, 5th Floor siyehua reply:
Quote: references to the 4th floor birdsaction reply:

Picture too big, do not use first recovered bitmap, followed by the oversized picture quality reduced. With BitmapFactory.Options relevant parameters.

Pictures Gaixiao no problem, that is to say the picture above recycling understand, Baidu got no clue

After using bitmap.recycle () (it should be so written)
Reply:
Upstairs positive solution
! ! !

No comments:

Post a Comment