0

ListViewに連絡先の写真を読み込むために、この質問の回答に従って、この質問のコードを変更しました。Photo_idを取得しており、loadContactPhoto(ContentResolver cr、long id)を使用して、連絡先のビットマップを取得するために使用しています。問題は、写真IDは常に異なりますが、ImageViewが新しい画像を取得していないことです。Contact._IDを使用してみましたが、それでも2つの連絡先のImageViewだけが連絡先の画像を取得し、両方とも間違っていました。以下に追加した新しい行にコメントしました。

編集後のコードは次のとおりです。

ContactStock:

public class ContactStock {

private String name;
private String number;
private Bitmap picture;


public ContactStock(String name, String number) {
    this.name = name;
    this.number = number;
}

public ContactStock(String name, String number, Bitmap photo) {
    this.name = name;
    this.number = number;
    this.picture = photo;
}

public void setName(String name) {
    this.name = name;
}

public void setNumber(String number) {
    this.number = number;
}

public String getName() {
    return this.name;
}

public String getNumber() {
    return this.number;
}

public void setPicture(Bitmap picture) { // NEW METHOD
    this.picture = picture;
}

public Bitmap getPicture() { // NEW METHOD
    return picture;
}
}

addlistfromcontact:

    public class addlistfromcontact extends Activity {
private ListView lst;
private List<ContactStock> contactstock;
private Cursor mCursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_contact_list);
    lst = (ListView) findViewById(R.id.tab_contact_list);
    contactstock = new ArrayList<ContactStock>();

    mCursor = managedQuery(ContactsContract.Data.CONTENT_URI, null,
            Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", null,
            ContactsContract.Data.DISPLAY_NAME + " ASC");

    int number = mCursor.getColumnIndex(Phone.NUMBER);
    int name = mCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);
    int id = mCursor.getColumnIndex(Contacts.PHOTO_ID); // NEW LINE

    while (mCursor.moveToNext()) {

        String phName = mCursor.getString(name);
        String phNumber = mCursor.getString(number);
        long phId = mCursor.getLong(id); // NEW LINE

        Bitmap phPhoto = loadContactPhoto(getContentResolver(), phId); // NEW LINE
        Log.d("phId=", phId + "");

        contactstock.add(new ContactStock(phName, phNumber, phPhoto)); // NEW LINE EDIT
    }
    lst.setAdapter(new ContactListAdapter(addlistfromcontact.this,
            contactstock));
}

public static Bitmap loadContactPhoto(ContentResolver cr, long id) { // NEW METHOD
    Uri uri = ContentUris.withAppendedId(
            ContactsContract.Contacts.CONTENT_URI, id);
    InputStream input = ContactsContract.Contacts
            .openContactPhotoInputStream(cr, uri);
    if (input == null) {
        return null;
    }
    return BitmapFactory.decodeStream(input);
}

    }

ContactListAdapter:

public class ContactListAdapter extends ArrayAdapter {
    private final Activity activity;
    private final List stocks;

    public ContactListAdapter(Activity activity, List objects) {
        super(activity, R.layout.listview_detail_tab_contact_list, objects);
        this.activity = activity;
        this.stocks = objects;
    }

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

        View rowView = convertView;
        ContactStockView sv = null;
        if (rowView == null) {
            // Get a new instance of the row layout view
            LayoutInflater inflater = activity.getLayoutInflater();
            rowView = inflater.inflate(
                    R.layout.listview_detail_tab_contact_list, null);

            // Hold the view objects in an object,
            // so they don't need to be re-fetched
            sv = new ContactStockView();
            sv.name = (TextView) rowView.findViewById(R.id.contact_name);
            sv.number = (TextView) rowView.findViewById(R.id.contact_number);
            sv.photo = (ImageView) rowView.findViewById(R.id.contact_photo);

            // Cache the view objects in the tag,
            // so they can be re-accessed later
            rowView.setTag(sv);
        } else {
            sv = (ContactStockView) rowView.getTag();
        }
        // Transfer the stock data from the data object
        // to the view objects
        ContactStock currentStock = (ContactStock) stocks.get(position);
        sv.name.setText(currentStock.getName());
        sv.number.setText(currentStock.getNumber());
        sv.photo.setImageBitmap(currentStock.getPicture()); // NEW LINE

        // TODO Auto-generated method stub
        return rowView;
    }

    protected static class ContactStockView {
        protected TextView name;
        protected TextView number;
        protected ImageView photo; // NEW LINE
    }
}
4

2 に答える 2

1

コードには2つの問題があります。最初のものは簡単に克服でき、他のものはより多くの作業が必要です。

簡単な方法から始めましょう。この方法ContactsContract.Contacts.openContactPhotoInputStream(cr, uri)では、写真のURIではなく連絡先のURIを使用します。これは、への呼び出しのIDがContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id)連絡先IDである必要があります。

Bitmapまた、結果セットを反復処理するときに、多くのオブジェクトを作成します。これをしないでください。これは最初は機能するかもしれませんがOutOfMemory、リストが長くなるとエラーでクラッシュする可能性があります。Bitmap必要な数のオブジェクトを作成してみてください。つまり、表示されている行のみです。リストビューをスクロールするときは、既存のビットマップをリサイクルする必要があります。

于 2012-05-26T15:54:54.207 に答える
0

UIがフリーズすることなく、すべての連絡先の写真を効率的に読み込む方法を見つけるのに多くの問題がありました。Androidでの作業を始めたときの画像の読み込みエラーでした。私の質問をご覧になる方は、こちらをよくご覧になることを強くお勧めします

これは、リストをスクロールしているときに「問題」が発生することなく、すべての処理データと画像のシームレスな読み込みを効率的に処理する方法を説明するサンプルアプリです。また、Androidでの作業を開始するための素晴らしいプロジェクトです。

具体的には、質問コードの連絡先画像部分について、APIは、連絡先の高解像度画像またはサムネイル を返す2つの簡単なメソッドをここで提供します。InputStreamあなたがしなければならないのは、を使用してそれをデコードすることBitmapFactory.decodeByteArray()です。OutOfMemoryErrorが発生しないように、アプリで使用する必要のある画像のサイズ(上記のリンクで説明されているもの)をロードすることを忘れないでください。

コードをさらに改善するために、ArrayAdapterをカスタムカーソルアダプターに置き換えて、必要なlistViewの行のみをその場でロードすることもできます。

于 2013-02-07T18:29:44.757 に答える