我執行下面的代碼在我adapter
當我面對的OutofMemory Error
。但問題的問題,這裏是我能夠加載只有第一個進入這裏的代碼形象,是在第0位gridview
。以下是我的代碼,請建議更改。OutOfMemory錯誤在GridView的
public class GridAdapter extends BaseAdapter {
String[] names;
int[] images;
Context context;
private static LayoutInflater inflater=null;
//private LruCache<String bitmap="",> memorucache;
private LruCache<String, Bitmap> mMemoryCache;
public GridAdapter(Context mainActivity,String[] _names,int[] _images){
names=_names;
images=_images;
context=mainActivity;
inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final int maxMemory= (int) (Runtime.getRuntime().maxMemory()/1024);
final int cacheSize=maxMemory/8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.getByteCount()/1024;
}
};
}
@Override
public int getCount() {
return names.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder=new Holder();
View rowView;
rowView=inflater.inflate(R.layout.gridlist,null);
holder.txtview= (TextView) rowView.findViewById(R.id.textView1);
holder.imageview= (ImageView) rowView.findViewById(R.id.imageView1);
holder.txtview.setText(names[position]);
loadBitmap(images[position], holder.imageview);
//holder.imageview.setImageResource(images[position]);
//rowView.setBackgroundColor(Color.parseColor(getColorCode()));
return rowView;
}
private void loadBitmap(int image, ImageView mimageview) {
final String imagekey=String.valueOf(image);
final Bitmap bitmap=getBitmapFromMemCache(imagekey);
if(bitmap != null){
mimageview.setImageBitmap(bitmap);
}else{
mimageview.setImageResource(R.drawable.ic_launcher);
final BitmapWorkerTask task=new BitmapWorkerTask(mimageview);
task.execute(image);
}
}
public class Holder{
TextView txtview;
ImageView imageview;
}
public String getColorCode(){
String[] colors = {"#6600CC","#3399FF","#FF9900","#003399","#CC6600"
,"#336600","#339933","#009999","#99CC00","#666633"
,"#666699","#333399","#003399","#993399","#990033"};
int random = (int)(Math.random()*14+1);
return colors[random];
}
class BitmapWorkerTask extends AsyncTask<Integer,Void,Bitmap>{
private final WeakReference<ImageView> imageViewReference;
public BitmapWorkerTask(ImageView mimageview) {
imageViewReference = new WeakReference<ImageView>(mimageview);;
}
@Override
protected Bitmap doInBackground(Integer... params) {
final Bitmap bitmap = decodeSampledBitmapFromResource(
context.getResources(), params[0], 100, 100);
addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
return bitmap;
//return null;
}
}
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
public Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}
public static Bitmap decodeSampledBitmapFromResource(Resources res,
int resId, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/(float) reqHeight);
final int widthRatio = Math.round((float) width/(float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio;
}
return inSampleSize;
}
}
看一看:https://androidactivity.wordpress.com/2011/09/24/solution-for-outofmemoryerror-bitmap-size-exceeds-vm-budget/ – camelCaseCoder
@ user2645941:嘗試使用Picasso library或sm其他在GridView中用於圖像緩存和加載。 – kevz
看一看:[鏈接](http://stackoverflow.com/a/27579930/2183890) – AKSiddique