0

連絡先画像への設定に問題がありますImageViewListView開発者の Web サイトから SO まで、いわゆる「解決策」をすべて試しましたが、どれも機能していないようです。

これは、私の CustomAdapter クラスであり、 http ListView: //pastebin.com/ABeK9nmTにデータを入力します。

これは、 http listview: //pastebin.com/FcBDprfGを保持している私の活動です。

アクティビティがそのlistview周りに移動できないオブジェクトを保持していることを知っておくことが重要です。コードを実行したときの問題は、デフォルトの写真レジスターのみが表示され、連絡先が表示されないことです。

アクティビティが実行されると、LogCatは 6 つの連絡先ごとに次のように表示します。

W/Resources(5997): Converting to string: TypedValue{t=0x12/d=0x0 a=3 r=0x7f080015}

エラーはまったく表示されません。誰かが私が間違っているかもしれないことを理解していますか?

他の SO "ソリューション" からのコピー アンド ペースト コーディングは行わないでください。私はそれらすべてを試しましたが、それらは下位の API 用であるか、別の回答からコピーしてポイントを取得したか、データが多すぎました。を使用する2.3.3を使用していますContacts.Contract

4

1 に答える 1

1

私はいくつかのことをお勧めします、そしてまたあなたが試みるかもしれない解決策とそれがまだ失敗するならば私に知らせたいです。

1)アプリケーションにSMSカスタムプロバイダーを使用していますが、ユーザーがOSを4.0以上に更新すると、失敗する可能性があります。

2)getView()にないすべてのカスタムプロバイダーにクエリを実行すると、プロセスとリストビューが高速化されるため、より適切です。

3)コードに問題があるのは、連絡先の画像を取得しようとしているときです。これは、以前に実装したように少し一般的で、ブログに記載されているようにわかりません。理由はわかりません。

4)ソリューションフロー:

  • 問い合わせたい電話番号があります。
  • 次に、その番号を取得して、電話帳コンテンツプロバイダーに名前を問い合わせます。
  • 名前がnullでない場合は、番号の特定のIDの取得に進むことがあります(IDを直接取得できるかどうかを確認し、名前を取得せずに実行できるかどうかを確認してください)
  • contactIdがnullでない場合は、画像をフェッチしてListViewに配置します。

nullチェックを配置し、前述のコードに従うことを忘れないでください。失敗した場合はお知らせください。

5)コード:

連絡先の名前を取得します。

/**
 * Method used to fetch CONTACT NAME FOR CONTACT NUMBER
 * 
 * @param number   :: Contact Number
 * @param context  :: Activity context
 * @return         :: returns CONTACT-NAME for CONTACT NUMBER
 */
private String getContactNameFromNumber(String number, Context context) 
{
    String contactName = null;
    Uri contactUri = null;
    String[] projection = null;
    String selection = null;
    Cursor cursorContactId = null;

    /*
     * Defining URI, Projection and Selection elements 
     */
    contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    projection = new String[]{Contacts._ID,Contacts.DISPLAY_NAME};
    selection = ContactsContract.CommonDataKinds.Phone.NUMBER + " = " + number;

    /*
     * Cursor iterator to fetch
     * particular ID
     */
    try
    {
        cursorContactId = context.getContentResolver().query(contactUri, projection, selection, null, null); 
        while (cursorContactId.moveToNext()) 
        { 
            contactName = cursorContactId.getString(cursorContactId.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        cursorContactId.close();
    }

    if(contactName != null)
    {
        return contactName;
    }
    else
    {
        return null;
    }
}

連絡先IDを取得します(名前がnullでない場合):

/**
 * Fetching Contact from a Number
 * @param number    - For which specific Id is required
 * @param context   - current context
 * @return          - ID as String
 */
private String getContactIdFromNumber(String number, Context context) 
{
    String contactId = null;
    Uri contactUri = null;
    String[] projection = null;
    String selection = null;
    Cursor cursorContactId = null;

    /*
     * Defining URI, Projection and Selection elements 
     */
    contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    projection = new String[]{Contacts._ID};
    selection = ContactsContract.CommonDataKinds.Phone.NUMBER + " = " + number;

    /*
     * Cursor iterator to fetch
     * particular ID
     */
    try
    {
        cursorContactId = context.getContentResolver().query(contactUri, projection, selection, null, null); 
        while (cursorContactId.moveToNext()) 
        { 
            contactId = cursorContactId.getString(cursorContactId.getColumnIndex(ContactsContract.Contacts._ID)); 
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        cursorContactId.close();
    }

    if(contactId != null)
    {
        return contactId;
    }
    else
    {
        return null;
    }
}

最後に連絡先画像を読み込みます(IDがnullでない場合。idとphoto_idに同じID値を渡します。このコードはさまざまなリンクを検索した後のSOから取得されます):

/**
 * Fetching contact picture from PhoneBook if available 
 * @param contentResolver   - Current Content Resolver context
 * @param id                - Specific Contact Id as retrieved
 * @param photo_id          - Same as Contact Id
 * @return                  - Contact Byte Array if present or null
 */
private byte[] loadContactPhoto(ContentResolver contentResolver, String id, String photo_id) 
{
    byte[] result = null;
    byte[] photoBytes = null;

    /*
     * Step 1 : Directly fetching with contactId or execute Step 2
     */
    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id));
    InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, uri);
    /*
     * Execute only if stream is not null
     */
    if (input != null) 
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try
        {
            int next = input.read();

            while (next > -1) 
            {
                bos.write(next);
                next = input.read();
            }

            bos.flush();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        result = bos.toByteArray();

        return result;
        // return BitmapFactory.decodeStream(input); // Open if want to return as Bitmap
    }
    else
    {
        Log.d("PHOTO","first try failed to load photo");
    }

    /*
     * Step 2: Image not fetched directly, fetching through traversing
     */
    Uri photoUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, Long.parseLong(photo_id));
    Cursor cursorImageFetch = contentResolver.query(photoUri, new String[] {ContactsContract.CommonDataKinds.Photo.PHOTO}, null, null, null);

    try 
    {
        if (cursorImageFetch.moveToFirst()) 
            photoBytes = cursorImageFetch.getBlob(0);
    } 
    catch (Exception e) 
    {
        e.printStackTrace();

    } finally {

        cursorImageFetch.close();
    }           

    if (photoBytes != null)
    {
        return photoBytes;
        //return BitmapFactory.decodeByteArray(photoBytes,0,photoBytes.length); // Open if want to return as Bitmap
    }
    else
    {
        Log.d("PHOTO","Picture of the contact not available");
        return getByteArray();
    }


}
于 2012-07-02T07:34:29.610 に答える