Sunday, June 6, 2010

[android-developers] Re: Bitmap memory handling with ImageView.setimage

While creating a bitmap, you should probably scale down the image
size. This is how you should do it.

BitmapFactory.Options bmpFactory = new BitmapFactory.Options();
bmpFactory.inSampleSize = 4;
Bitmap bmp = BitmapFactory.decodeFile(path, bmpFactory);

The sample size 4, will scale down the bitmap to 1/4 of the original
size. This will, in turn, reduce the size of the Bitmap object by a
factor of 4. Hope this helps.

Thanks and Regards,
Kumar Bibek

On Jun 5, 3:22 am, Nathan <critter...@crittermap.com> wrote:
> I've read a lot of previous messages on this topic but couldn't find
> universal agreement on the whole.
>
> I can't reproduce this in house, but a customer got an
> OutOfMemoryError when using an icon chooser dialog.
>
> This dialog is much like the Grid1 example - only it's in a dialog.
>
> The bitmap memory seems to run out after running the dialog a few
> times for a customer, resulting in
>  java.lang.OutOfMemoryError: bitmap size exceeds VM budget
>
>         public View getView(int pos, View convertView, ViewGroup parent) {
>         ImageView imageView;
>         if (convertView == null) {
>             imageView = new ImageView(mContext);
>             //imageView.setLayoutParams(new GridView.LayoutParams(40,
> 40));
>
>             imageView.setAdjustViewBounds(false);
>             imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
>             imageView.setPadding(4, 4, 4, 4);
>         } else {
>
>             imageView = (ImageView) convertView;
>         }
>
>         imageView.setImageResource(idlist.get(pos).value);
>         imageView.setTag(idlist.get(pos).key);
>         return imageView;
>         }
>
> Should I try to call recycle on the bitmap from an ImageView when I
> reuse the view? Or would the ImageView do that anyway?
>
> But even if I do that, that's only a small portion of the bitmaps
> because most of them are seen without scrolling. The Adapter and the
> GridView are both local variables and therefore shouldn't be
> referenced after the dialog is dismissed.
>
> I do believe that it should be possible to use many bitmaps in an app
> - Google Maps does it, Picture Gallery does it. But maybe I need to
> learn more rules. Some say you should call bitmap.recycle, some say
> you shouldn't have to. Many say calling gc.collect will make it worse,
> etc.
>
> Nathan

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment