Dynamically adding Images to GridView

by Gaurav » Sat, 14 Mar 2009 02:09:50 GMT

Sponsored Links
 I have a Gallery and a GridView in my application. Gallery's adapter
is based on thumbnail cursor created through query using

I use this Gallery to choose images to display in GridView. Whenever
the user clicks on an  image in the Gallery I have to add this image
to GridView. Currently, I use an ArrayAdapter<Integer> to store the
images in the GridView. The value stored as objects in the
ArrayAdapter is the _ID of the thumbnail and getView on GridView looks
like this:

        public View getView(int position, View convertView, ViewGroup
parent) {

                ImageView i = new ImageView(mContext);

                System.out.println("I am in get view:" + position);
                int thumbnailId = getItem(position);
"" + thumbnailId));
                i.setLayoutParams(new GridView.LayoutParams(80, 80));
                return i;

Whenever I click on an item in Gallery, getView is called for all
visible positions in the GridView (9 images). After, 12-13 images are
added to the gridview and I scroll up and down, I get an
OutOfMemoryError. Can someone suggest how I can rework to avoid memory
errors here.

03-13 02:29:45.456: ERROR/dalvikvm-heap(487): 338322-byte external
allocation too large for this process.
03-13 02:29:45.456: ERROR/(487): VM won't let us allocate 338322 bytes
03-13 02:29:45.466: DEBUG/skia(487): xxxxxxxxxxxxxxxxxxxx
allocPixelRef failed
03-13 02:29:58.845: DEBUG/AndroidRuntime(487): Shutting down VM
03-13 02:29:58.845: WARN/dalvikvm(487): threadid=3: thread exiting
with uncaught exception (group=0x4000fe68)
03-13 02:29:58.845: ERROR/AndroidRuntime(487): Uncaught handler:
thread main exiting due to uncaught exception
03-13 02:29:59.045: ERROR/AndroidRuntime(487):
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
03-13 02:29:59.045: ERROR/AndroidRuntime(487):     at
android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
03-13 02:29:59.045: ERROR/AndroidRuntime(487):     at
03-13 02:29:59.045: ERROR/AndroidRuntime(487):     at
03-13 02:29:59.045: ERROR/AndroidRuntime(487):     at
03-13 02:29:59.045: ERROR/AndroidRuntime(487):     at


Other Threads

1. Modal dialog

I am new to Android. How do I popup modal dialog? By modal I mean I
call something like showModalDialog function and it will not return
until the dialog is closed and I can get what button was used to close
the dialog.

2. Scaling icons on MapActivity with zoom

Hey all,

I been messing around with the MapActivity stuff. Pretty nice how easy it is
to add clickable icons/images to the map and make them mean something. That
said I notice that as you zoom in and out, they don't scale automatically.
Is there a way to achieve this as part of the SDK? I've not found any way to
enable this yet. Or do I need to listen for zoom events and somehow resize
my icons.


3. SDK addon sample problem.

4. How to open home screen?

5. Click on Notification does not work as expected... (see screencast!):(

6. How to display XML

7. TableLayout will not draw new row addition