0

I created an app to show one contact photo by using his/her number , but I got nothing my result just a blank page , this is my main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <QuickContactBadge
        android:id="@+id/quickContactBadge1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

and this is my MainActivity.java :

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String phoneNumber = "XXXXXXXXXXX";
        QuickContactBadge badge = (QuickContactBadge) findViewById(R.id.quickContactBadge1);
        new QuickContactHelper(this, badge, phoneNumber).addThumbnail();
    }



}

and this is QuickContactHelper.java:

public class QuickContactHelper {

    private static final String[] PHOTO_ID_PROJECTION = new String[] { ContactsContract.Contacts.PHOTO_ID };

    private static final String[] PHOTO_BITMAP_PROJECTION = new String[] { ContactsContract.CommonDataKinds.Photo.PHOTO };

    private final QuickContactBadge badge;

    private final String phoneNumber;

    private final ContentResolver contentResolver;

    public QuickContactHelper(final Context context,
            final QuickContactBadge badge, final String phoneNumber) {

        this.badge = badge;
        this.phoneNumber = phoneNumber;
        contentResolver = context.getContentResolver();

    }

    public void addThumbnail() {

        final Integer thumbnailId = fetchThumbnailId();
        if (thumbnailId != null) {
            final Bitmap thumbnail = fetchThumbnail(thumbnailId);
            if (thumbnail != null) {
                badge.setImageBitmap(thumbnail);
            }
        }

    }

    private Integer fetchThumbnailId() {

        final Uri uri = Uri.withAppendedPath(
                ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,
                Uri.encode(phoneNumber));
        final Cursor cursor = contentResolver.query(uri, PHOTO_ID_PROJECTION,
                null, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");

        try {
            Integer thumbnailId = null;
            if (cursor.moveToFirst()) {
                thumbnailId = cursor.getInt(cursor
                        .getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
            }
            return thumbnailId;
        } finally {
            cursor.close();
        }

    }

    final Bitmap fetchThumbnail(final int thumbnailId) {

        final Uri uri = ContentUris.withAppendedId(
                ContactsContract.Data.CONTENT_URI, thumbnailId);
        final Cursor cursor = contentResolver.query(uri,
                PHOTO_BITMAP_PROJECTION, null, null, null);

        try {
            Bitmap thumbnail = null;
            if (cursor.moveToFirst()) {
                final byte[] thumbnailBytes = cursor.getBlob(0);
                if (thumbnailBytes != null) {
                    thumbnail = BitmapFactory.decodeByteArray(thumbnailBytes,
                            0, thumbnailBytes.length);
                }
            }
            return thumbnail;
        } finally {
            cursor.close();
        }

    }

}

and this is permission for read contacts:

<uses-permission android:name="android.permission.READ_CONTACTS"/>

now what's my error while there is no exception no error , and I always getting a blank page so how to handle this error ? and this is my result :

Blank Page

ROR
  • 271
  • 3
  • 29
  • there's a discussion here (which includes your code) which offers other working solutions: http://stackoverflow.com/questions/2383580/how-do-i-load-a-contact-photo – snowdragon Jun 23 '14 at 13:08
  • That's right , here http://stackoverflow.com/questions/2383580/how-do-i-load-a-contact-photo , the answer accepted but it's not working till now , if it's work why not working for me ? this is my question – ROR Jun 23 '14 at 13:13
  • from what I saw, some people said the accepted answer didn't work for them (perhaps related to FB contact sync?), and also in the Android docs they show a different way of doing this: http://developer.android.com/training/contacts-provider/display-contact-badge.html]. I suggest you give another solution a try. – snowdragon Jun 23 '14 at 13:28
  • This one is based on the Android docs link I shared: http://stackoverflow.com/a/23120749/1469004 – snowdragon Jun 23 '14 at 13:30
  • just let me try for this , yeah you're unfortunately – ROR Jun 23 '14 at 13:35
  • @snowdragon really it's tooooo difficult I just wanna retrieve one photo for one user , now I have uri (content//com.android.contacts/contacts/1782/photo) this is my uri when put this uri in imageview nothing – ROR Jun 23 '14 at 13:43

0 Answers0