Whenever you call cr.query, it returns a cursor that _you_ are supposed to
close.  The general pattern is this:

Cursor c = cr.query(...)
try {
  c.move...
} finally {
  c.close();
}

This assures that the cursor is released even if the code inside the try
clause throws an exception.

Cheers,
Dmitri

On May 30, 2010 5:43 AM, "mike" <[email protected]> wrote:
> hi guys,
>
> i looking for a solution for this issue for more than 2 weeks now.
> even i have uploaded this issue previously but no one is helping me
> out.
>
> i have a SimpleCursorAdapter which will load all the contacts in the
> address book with there contact images if they don't have an image a
> default image will be displayed.
>
> other than this another image will be displayed.
>
> so altogether Contact image/ Contact Name/Contact Number and another
> image will be displayed.
>
> i have about 400 + contacts.
>
> so when i'm scrolling down the list i'm getting this Memory issue.
>
> so when i removed all the images it works fine.
>
> this is my code
>
>
> import java.io.ByteArrayInputStream;
> import java.util.HashMap;
> import java.util.List;
> import java.util.Map;
> import com.sabretch.mobility.colorEyeD3.R;
> import com.sabretch.mobility.colorEyeD3.Constants.Constants;
> import com.sabretch.mobility.colorEyeD3.custom.CustomScreen;
> import
> com.sabretch.mobility.colorEyeD3.database.TableDetails.MetaData;
> import com.sabretch.mobility.colorEyeD3.text.compose.Compose;
> import android.app.ListActivity;
> import android.app.ProgressDialog;
> import android.app.SearchManager;
> import android.content.ContentUris;
> import android.content.Context;
> import android.content.Intent;
> import android.database.Cursor;
> import android.graphics.Bitmap;
> import android.graphics.drawable.BitmapDrawable;
> import android.net.Uri;
> import android.os.Bundle;
> import android.provider.Contacts.Phones;
> import android.provider.Contacts.Photos;
> import android.util.Log;
> import android.view.LayoutInflater;
> import android.view.View;
> import android.view.ViewGroup;
> import android.widget.Filterable;
> import android.widget.ImageView;
> import android.widget.ListView;
> import android.widget.SimpleCursorAdapter;
> import android.widget.TextView;
>
> public class Contacts extends ListActivity {
> static final String[] cat = Constants.DEFAULT_CATEGORIES;
> static int[] id;
> static List<Long> contactId;
> DisplayContacts dispCont;
> List<String> number;
> Map<String, String> combination = new HashMap<String, String>();
> String isDisplayDetails;
> ProgressDialog dialog;
>
> private class DisplayContacts extends SimpleCursorAdapter {
> private LayoutInflater mInflater;
> Context mCtx;
>
> public DisplayContacts(Context context, int layout, Cursor c,
> String[] from, int[] to) {
> super(context, layout, c, from, to);
> // TODO Auto-generated constructor stub
> mInflater = LayoutInflater.from(context);
> this.mCtx = context;
> }
>
> @Override
> public View newView(Context context, Cursor cursor, ViewGroup
> parent) {
> // TODO Auto-generated method stub
> Cursor c = getCursor();
> // int idCol = c.getColumnIndex(Phones._ID);
> int nameCol = c.getColumnIndex(Phones.NAME);
> int numCol = c.getColumnIndex(Phones.NUMBER);
> int foreign = c.getColumnIndex(Phones.PERSON_ID);
> String name = c.getString(nameCol);
> String number = c.getString(numCol);
> // long id = c.getLong(idCol);
> long phoneForeign = c.getLong(foreign);
> View v = mInflater.inflate(R.layout.contacts, parent, false);
> TextView name_text = (TextView) v.findViewById(R.id.contactName);
>
> if (name_text != null) {
> name_text.setText(name);
> }
>
> TextView num_text = (TextView) v.findViewById(R.id.number);
> if (num_text != null) {
> num_text.setText(number);
> }
> // set the profile picture
> ImageView profile = (ImageView) v.findViewById(R.id.imgContact);
> if (profile != null) {
> // retrieve the contact photo as a Bitmap
> // Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, id);
> Cursor cur = getContentResolver().query(Photos.CONTENT_URI,
> null, Photos.PERSON_ID + "='" + phoneForeign + "'",
> null, null);
> byte[] b = null;
> if (cur != null) {
> if (cur.moveToNext()) {
> int imgColumn = cur.getColumnIndex(Photos.DATA);
> b = cur.getBlob(imgColumn);
> }
> }
> Bitmap bm = null;
> if (b != null) {
> ByteArrayInputStream bytes = new ByteArrayInputStream(b);
> BitmapDrawable bmd = new BitmapDrawable(bytes);
> bm = bmd.getBitmap();
> profile.setImageBitmap(bm);
> } else {
> profile.setImageResource(R.drawable.defaultcontact);
> }
> }
> ImageView indicator = (ImageView) v.findViewById(R.id.is);
> boolean b = isAssign(number);
> if (b) {
> // set the profile picture
> indicator.setImageResource(R.drawable.ok);
> } else {
> indicator.setImageResource(R.drawable.delete);
> }
> dialog.dismiss();
> return v;
> }
>
> protected boolean isAssign(String number) {
> boolean b = false;
> Uri books = MetaData.CONTENT_URI;
> // Log.d("Authority", books.getAuthority());
> Cursor cur = managedQuery(books, null, MetaData.MOBILE_NUMBER
> + "='" + number + "'", null, MetaData.CAT_NAME + " DESC");
> startManagingCursor(cur);
> if (cur != null) {
> if (cur.moveToNext()) {
> combination.put(number.trim(), cur.getString(cur
> .getColumnIndex(MetaData.COMBINATION)));
> isDisplayDetails = cur.getString(cur
> .getColumnIndex(MetaData.IS_HIDE));
> b = true;
> return b;
> } else {
> return b;
> }
> }
> return b;
> }
>
> @Override
> public void bindView(View view, Context context, Cursor c) {
> // TODO Auto-generated method stub
> // int idCol = c.getColumnIndex(Phones._ID);
> int nameCol = c.getColumnIndex(Phones.NAME);
> int numCol = c.getColumnIndex(Phones.NUMBER);
> int foreign = c.getColumnIndex(Phones.PERSON_ID);
> String name = c.getString(nameCol);
> String number = c.getString(numCol);
> // long id = c.getLong(idCol);
> long phoneForeign = c.getLong(foreign);
> // View v = mInflater.inflate(R.layout.contacts, parent, false);
> TextView name_text = (TextView)
> view.findViewById(R.id.contactName);
> if (name_text != null) {
> name_text.setText(name);
> }
>
> TextView num_text = (TextView) view.findViewById(R.id.number);
> if (num_text != null) {
> num_text.setText(number);
> }
>
> // set the profile picture
> ImageView profile = (ImageView) view.findViewById(R.id.imgContact);
> if (profile != null) {
> // Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, id);
> Cursor cur = getContentResolver().query(Photos.CONTENT_URI,
> null, Photos.PERSON_ID + "='" + phoneForeign + "'",
> null, null);
> byte[] b = null;
> if (cur != null) {
> if (cur.moveToNext()) {
> int imgColumn = cur.getColumnIndex(Photos.DATA);
> b = cur.getBlob(imgColumn);
> }
> }
> Bitmap bm = null;
> if (b != null) {
> ByteArrayInputStream bytes = new ByteArrayInputStream(b);
> BitmapDrawable bmd = new BitmapDrawable(bytes);
> bm = bmd.getBitmap();
> profile.setImageBitmap(bm);
> } else {
> profile.setImageResource(R.drawable.defaultcontact);
> }
> ImageView indicator = (ImageView) view.findViewById(R.id.is);
> boolean assign = isAssign(number);
> if (assign) {
> // set the profile picture
> indicator.setImageResource(R.drawable.ok);
> } else {
> indicator.setImageResource(R.drawable.delete);
> }
> }
> }
>
> @Override
> public Object getItem(int position) {
> // TODO Auto-generated method stub
> return super.getItem(position);
> }
>
> }
>
> @Override
> protected void onCreate(Bundle savedInstanceState) {
> // TODO Auto-generated method stub
> super.onCreate(savedInstanceState);
> setTitle("Color Eye D - Contacts");
> dialog = ProgressDialog.show(Contacts.this, "",
> "Loading. Please wait...", true);
>
>
> Cursor cursor = getContentResolver().query(Phones.CONTENT_URI, null,
> null, null, Phones.NAME + " ASC");
> startManagingCursor(cursor);
>
> String[] columns = new String[] { Phones.NAME, Phones.NUMBER };
> int[] names = new int[] { R.id.contactName, R.id.number };
> dispCont = new DisplayContacts(this, R.layout.contacts, cursor,
> columns, names);
> setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
> onNewIntent(getIntent());
> setListAdapter(dispCont);
> // setListAdapter(new DisplayContacts(this));
> registerForContextMenu(getListView());
>
> }
>
> @Override
> protected void onListItemClick(ListView l, View v, int position, long
> id) {
> // TODO Auto-generated method stub
> Cursor c = (Cursor) dispCont.getItem(position);
> long phoneId = c.getLong(c.getColumnIndex(Phones._ID));
> String composeName = null;
> Uri uri = ContentUris.withAppendedId(Phones.CONTENT_URI, phoneId);
> Cursor cur = managedQuery(uri, null, null, null, null);
> String phoneNumber = null;
> if (cur.moveToFirst()) {
> Constants.selectedName = cur.getString(cur
> .getColumnIndex(Phones.NAME));
> composeName = Constants.selectedName;
> int phoneColumn = cur.getColumnIndex(Phones.NUMBER);
> do {
> // Get the field values
> phoneNumber = cur.getString(phoneColumn);
> } while (cur.moveToNext());
> }
>
> if (Constants.isCompose) {
> String combina = null;
> if (phoneNumber != null) {
> Constants.selectedName = null;
> combina = combination.get(phoneNumber.trim());
> }
> String[] data = new String[] { combina, composeName, phoneNumber,
> isDisplayDetails };
> Intent comp = new Intent(Contacts.this, Compose.class);
> comp.putExtra("Combination", data);
> startActivity(comp);
> finish();
> } else {
> Intent i = new Intent(Contacts.this, CustomScreen.class);
> Bundle b = new Bundle();
> Constants.mobile = phoneNumber;
> b.putString("Number", phoneNumber);
> i.putExtras(b);
> startActivity(i);
> finish();
> }
> }
> }
>
>
> regards,
> Randika
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]<android-developers%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

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

Reply via email to