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. Can anyone recommend a good OpenGL ES book? Options

@Emre: do not hijack threads! Have a question? Then make your own


2. How to use Webview.loadUrl(String url) when my html file is local

Hi Everyone,

how do I open a a local html-page (named, for instance "test.html") in a

where should i keep this keep html page with images in project and how to
Webview.loadUrl(String url) ?

Thanks in advance


3. how to distinguish java process

4. How to pass a Context variable from one Activity to another ?

5. How to use NDK to do AMR encoding

6. Dynamic class loading - how?

7. Is it possible for an app to replace the NotificationManager?