Am I leaking bitmaps from adapter's getView ?

by Alexey » Wed, 01 Apr 2009 09:41:54 GMT


Sponsored Links
 Hi All , I definitely have a memory leak problem and i'm trying to
figure out there. After 6-10 config changes i'm getting out of memory
in regards to  BitmapFactory.  I'm kind of suspect that this is going
on within adapter.
 I have a listview . ListItems are an thumbnail image ( ImageView )
and TextView for text.

Here is the getView code.

                                if (convertView != null )
                                        newView = convertView;
                                else {
                                        LayoutInflater layout = 
LayoutInflater.from(mContext);
                                        newView = 
layout.inflate(R.layout.list_item, null);
                                }

                                TextView tView = (TextView) 
newView.findViewById(R.id.title);

                                tView.setText(nItem.Title);

                                ImageView iView = (ImageView) 
newView.findViewById(R.id.image);

                                Bitmap thumbnail = nItem.getThumbnailBitmap();
                                if (thumbnail != null) {
                                        iView.setImageBitmap(thumbnail);


and get ThumbnailBitmap() is basically
BitmapFactory.decodeByteArray ..  Is it possible that i'm leaking
drawables ? Heap is not increasing by the way .
--~--~---------~--~----~------------~-------~--~----~



Am I leaking bitmaps from adapter's getView ?

by jefferyfry » Thu, 02 Apr 2009 08:15:22 GMT


 Are you recycling your bitmaps?  I'm not sure what your
getThumbnailBitmap is doing, but I have something that sounds similar
where I rescale my bitmaps like so:

        public static Bitmap getBitmap(Context context,String photoUriPath)
throws Exception {
                Uri photoUri = Uri.parse(photoUriPath);
                InputStream photoStream = context.getContentResolver
().openInputStream(photoUri);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize=2;
                Bitmap photoBitmap = BitmapFactory.decodeStream
(photoStream,null,options);
                int h = photoBitmap.getHeight();
                int w = photoBitmap.getWidth();
                if((w>h)&&(w>128)){
                        double ratio = 128d/w;
                        w=128;
                        h=(int)(ratio*h);
                }
                else if((h>w)&&(h>128)){
                        double ratio = 128d/h;
                        h=128;
                        w=(int)(ratio*w);
                }
                Bitmap scaled = Bitmap.createScaledBitmap(photoBitmap, w, h, 
true);
                photoBitmap.recycle();
                return scaled;
        }

I make sure I call recycle() on the original bitmap to conserve
memory.



--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Am I leaking bitmaps from adapter's getView ?

by Alexey » Sun, 12 Apr 2009 05:42:20 GMT


 thanks ! I'll go through my images again to make sure - but i found my
problem  data observer that i forgot to unregistered so now it's much
better.




--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Height adjustment bug with ImageSwitcher

I am using ImageSwitcher to show a set of photos. Each photo has its
unique caption (TextView) below it.  As the user switches from photo A
to the photo B, the following behavior is observed:

If photo B's height is greater than photo A, then caption for B is
shown correctly.
But if photo B's height is less than photo A, then the caption of
photo B would be shown in the same y position as the caption for photo
A. The result is empty space between the photo and the caption.  This
should not happen as I am using "wrap_content" for the layout_height
attribute of ImageSwitcher.  The XML layout file s{*filter*}t is:

<ScrollView xmlns:android=" http://www.***.com/ ;
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbars="none">

<LinearLayout xmlns:android=" http://www.***.com/ 
android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <Gallery android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:gravity="center_vertical"
        android:spacing="13"
    />
    <RelativeLayout xmlns:android=" http://www.***.com/ 
android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <ImageSwitcher android:id="@+id/switcher"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
            />
            <TextView  android:id="@+id/photo_caption"
                    android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="10sp"
                android:textAlign="start"
                android:layout_below="@id/switcher"
                android:background="#77000000"
            />

I tried to use LinearLayout instead of RelativeLayout, but same
results.

Please, if anyone has a workaround, let me know.  I appreciate the
help.
--~--~---------~--~----~------------~-------~--~----~

2. Displaying data from the database in a Spinner

Hi,

I'm trying to display items from a database in a Spinner. I've read
other posts on this but my items still aren't showing up and none of
the posts show a complete solution.

To help debug the error, I added a ListView and the items from the
same db call display without any problem in the list. Thus, there's
some little detail that I can't find in my code for the Spinner. I'd
certainly appreciate someone pointing out the error.

To try to help me debug the code, after selecting an item from the
"invisible" list, I print out the selected item and it displays:
[EMAIL PROTECTED]

I can see that the spinner has lots of items and I can scroll through
them but they don't display anything.

Code:

public class TestListCursor extends ListActivity {
    private DBInterface mDbHelper;
    private Cursor notesCursor,
                           spinnerCursor;
    Spinner spnItems;
    TextView tvItem;
    SimpleCursorAdapter _itemsAdapter = null;
    Button btnSel;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.notes_list);
        this.spnItems = (Spinner) findViewById(R.id.spn_NL_items);
        this.tvItem = (TextView) findViewById(R.id.tvNLSelection);
        this.btnSel = (Button) findViewById(R.id.btnNL);

        mDbHelper = new DBInterface(this);
        fillSpinner();
        fillData();

        this.btnSel.setOnClickListener (new View.OnClickListener() {
            public void onClick(View v) {
 
TestListCursor.this.tvItem.setText(TestListCursor.this.spnItems.getSelectedItem().toString());
            }
        });

    }



    private void fillData() {
        // Get all of the rows from the database and create the item
list
        notesCursor = null;
        notesCursor = mDbHelper.getItemsCursor();


        // Create an array to specify the fields we want to display in
the list (only TITLE)
        String[] from = new String[]{DbAdapter.ROUTE_NAME};

        // and an array of the fields we want to bind those fields to
(in this case just text1)
        int[] to = new int[]{R.id.text1};
        int i = 0;
                 if (notesCursor != null) {
                    startManagingCursor(notesCursor);
                // Now create a simple cursor adapter and set it to display
                SimpleCursorAdapter notes =
                            new SimpleCursorAdapter(this, R.layout.notes_row,
notesCursor, from, to);
                setListAdapter(notes);
                 }
    }

    private void fillSpinner() {
          spinnerCursor = mDbHelper.getItemsCursor();
              if (this.spinnerCursor != null) {
                         MMFLog.log("Workout fillItemsSpinner cursor items: " +
this.spinnerCursor.count());
                         startManagingCursor(this.spinnerCursor);
                         this.spinnerCursor.next();
                         // Create an array to specify the fields we want to 
display
in the spinner (only Name)
                         String[] names = new String[] {DbAdapter.ROUTE_NAME};
                         int[] to = new int[]
{ android.R.layout.simple_spinner_item };

                         // Now create a simple cursor adapter and set it to 
display
                         this._itemsAdapter =
                                    new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item, this.spinnerCursor, names, to);
        
this._itemsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                         this._itemsAdapter.bindView(this.spnItems, this,
this.spinnerCursor);
                         this.spnItems.setAdapter(this._itemsAdapter);
                 } else {
                         Toast.makeText(this, "error: items cursor is null",
Toast.LENGTH_SHORT);
                 }
    }

}


--------------------------
xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
        android:layout_height="275px"
        android:layout_width="wrap_content"
        android:orientation="vertical"
        >
    <Button android:id="@+id/btnNL"
          android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Spinner Selection"/>

    <Spinner android:id="@+id/spn_NL_items"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>

    <TextView android:id="@+id/tvNLSelection"
            android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

    <ListView android:id="@+id/android:list"
          android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        <TextView android:id="@+id/android:empty"
          android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="No Notes!"/>

</LinearLayout>

If you see the error or if you have a good working example that you'd
be willing to post, I'd certainly appreciate the feedback.

AN


--~--~---------~--~----~------------~-------~--~----~

3. List of images

4. Need help in debugging null pointer exception

5. Browser zoom

6. Can't Install Apk on Emulator on a Mac!

7. No way to have an activity be always-on-top?