Wednesday 15 June 2011

android - How to avoid "outofmemory" when download image in gridview? -


I have tried to show a lot of images in Gridview. There are two activities. Both of them have gridviews whose pictures are when I start only one of them. There are no questions, but when I start the second, then the problem of "out problem" occurs PS: To use this stackoverflow is my first and my english is me poor.forgive ~ ~

 < Code> Public category HeroGridAdapter extends the BaseAdapter {Personal context mContext ;. Private Inc [] mImages; Private string [] mNames; Private string [] M shorts; Private layoutInflator aminflator; Private liner layout. Layout Parameters; Private static map & lt; Integer, softReference & lt; Bitmap & gt; & Gt; ImageCache = New Hashmap & lt; Integer, soft reference & lt; Bitmap & gt; & Gt; (); Public Hero Grid Adapter (Reference C, Ent [] Images, String [] Name, String [] Shorts) {this.mContext = c; this. Images = Pictures; This.mNames = names; this. Shorts = shorts; This.mInflater = Layout Inflater.from (C); This.params = New LinearLayout.LayoutParam (45,45); This.params.gravity = Gravity.CENTER; } @ Override Public In Receipt () {Return MIMsLine; } @OverWide public object found i.Temat {return status; } @ Override Public Long ITMID (Status of Intake) {Return Status; } @ Override public view getView (view the status of the int, view conversion, see parent parent) {item view toggle viewTag; If (convertView == empty) {convertView = mInflater.inflate (R.layout.hero_portrait_grid, null); ViewTag = newViewViewTag ((imageView) convertView.findViewById (R.id.ivHeroPor), (TextView) convertView.findViewById (R.id.tvHeroPor), (TextView) convertView.findViewById (R.id.tvHeroShort)); ConvertView.setTag (viewTag); } And {viewTag = (item view tog) Convertview.gettag (); } AddBitmapToCache (mimages [position]); ViewTag.name.setText (mNames [status]); ViewTag.shortName.setText (mShorts [status]); ViewTag.icon.setImageBitmap (getBitmap (mImages [status])); Return Convertview; } Category ItemViewTag {Personal ImageView icon; Private text view name; Private text view abbreviations; Public item view tog (image view icon, textview name, text view short name) {this.icon = icon; This.name = name; This.shortName = shortName; }} Public static int calculateInSampleSize (BitmapFactory.Options option, integer reqWidth, integer reqHeight) {// Raw height and image final integer height = options.outHeight width; Final full width = options.outWidth; Int inSampleSize = 1; If (height> reqHeight || width> reqWidth) {// Requested height // width final integer heightRatio = mathematics.gol ((boat) height / (boat) reqHeight) calculate the ratio of height and width ; Final integer width = zodiac = mathematical (Float width / (float) reset); // Choose the smallest proportion as the sample value, this // // will be the final image // with both dimensions greater than or equal to the height and width of the request. InSampleSize = heightRatio & lt; WidthRatio? Height row width } Return to sample size; } Public static bitmap decodeSampledBitmapFromResource (Resource Race, Integer Resid, integer reqWidth, integer reqHeight) {// inJustDecodeBounds with first decode = Dimensions Final BitmapFactory.Options option = new BitmapFactory.Options (checked) true; Options.inJustDecodeBounds = True; Bitmap bitmap = bitmapfender Decode processing (RSID, options, options); // Change in the calculation example options.inSampleSize = Count InSampleSize (option, reqWidth, reqHyight); //InSampleSize set with DeanDit bitmap options.inJustDecodeBounds = false; Back Bitmapfichture Decode processing (res, resId, option); } Public Zero addBitmapToCache (Integer Resid) {bitmap bitmap = decodeSampledBitmapFromResource (mContext.getResources (), Resid, 45, 45); SoftReference & LT; Bitmap & gt; Softbitmap = new soft reference & lt; Bitmap & gt; (Bitmap); ImageCache.put (resId, softBitmap); } Public Static Bitmap getBitmap {Int Imagekey} {Soft References & lt; Bitmap & gt; Softbitmap = imageCache.get (imageKey); If (soft bitmap == zero) {return tap; } Bitmap bitmap = soft bitmap.gate (); Return bitmap; }}    

I think it can help you < P> And there is a pretty good sample that you can do on developer.android.com which handles all bitmap memory.

No comments:

Post a Comment