将联系人图像加载到位图中 [英] Load Contact Image Into Bitmap

查看:27
本文介绍了将联系人图像加载到位图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用来从电话号码获取联系人图像缩略图的 Uri 的函数.:

the function i use to get the Uri of the contact image thumbnail from the phone no. :

public static Uri getPhotoURIFromAddress(Context activity, String address) {
    String contactId = getContactIdFromAddress(activity, address);

    ContentResolver contentResolver = activity.getContentResolver();
    try {
        Cursor cursor = contentResolver
                .query(ContactsContract.Data.CONTENT_URI,
                        null,
                        ContactsContract.Data.CONTACT_ID
                                + "="
                                + contactId
                                + " AND "

                                + ContactsContract.Data.MIMETYPE
                                + "='"
                                + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
                                + "'", null, null);

        if (cursor != null) {
            if (!cursor.moveToFirst()) {
                return null; // no photo
            }
        } else {
            return null; // error in cursor process
        }
        cursor.close();

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    Uri person = ContentUris.withAppendedId(
            ContactsContract.Contacts.CONTENT_URI, Long.valueOf(contactId));
    return Uri.withAppendedPath(person,
            ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}

它将以 :

content://com.android.contacts/contacts/799/photo

现在,如果我在带有 setImageUri(Uri) 函数的 ImageView 中使用这个 Uri,它就可以工作了.

now, if i use this Uri in an ImageView with the setImageUri(Uri) function, it works.

但是加载位图是一个问题.我正在使用的功能是:

But loading a Bitmap is a problem. the function I'm using is :

public static Bitmap getContactBitmapFromURI(Context context, Uri uri) {
        InputStream input = ContactsContract.Contacts
                .openContactPhotoInputStream(context.getContentResolver(), uri);
        if (input == null) {
            return null;
        }
        return BitmapFactory.decodeStream(input);
    }

总是崩溃.LogCat 是:

which always crashes. LogCat is :

12-13 21:40:26.016: E/AndroidRuntime(9076): FATAL EXCEPTION: main
12-13 21:40:26.016: E/AndroidRuntime(9076): java.lang.RuntimeException: Unable to start receiver com.daksh.fss.SMSReceiver: java.lang.IllegalArgumentException: URI: content://com.android.contacts/contacts/799/photo/photo, calling user: com.daksh.fss, calling package:com.daksh.fss
12-13 21:40:26.016: E/AndroidRuntime(9076):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2362)
12-13 21:40:26.016: E/AndroidRuntime(9076):     at android.app.ActivityThread.access$1500(ActivityThread.java:142)
12-13 21:40:26.016: E/AndroidRuntime(9076):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
12-13 21:40:26.016: E/AndroidRuntime(9076):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 21:40:26.016: E/AndroidRuntime(9076):     at android.os.Looper.loop(Looper.java:137)
12-13 21:40:26.016: E/AndroidRuntime(9076):     at android.app.ActivityThread.main(ActivityThread.java:4931)
12-13 21:40:26.016: E/AndroidRuntime(9076):     at java.lang.reflect.Method.invokeNative(Native Method)
12-13 21:40:26.016: E/AndroidRuntime(9076):     at java.lang.reflect.Method.invoke(Method.java:511)
12-13 21:40:26.016: E/AndroidRuntime(9076):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
12-13 21:40:26.016: E/AndroidRuntime(9076):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
12-13 21:40:26.016: E/AndroidRuntime(9076):     at dalvik.system.NativeStart.main(Native Method)
12-13 21:40:26.016: E/AndroidRuntime(9076): Caused by: java.lang.IllegalArgumentException: URI: content://com.android.contacts/contacts/799/photo/photo, calling user: com.daksh.fss, calling package:com.daksh.fss
12-13 21:40:26.016: E/AndroidRuntime(9076):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:170)
12-13 21:40:26.016: E/AndroidRuntime(9076):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
12-13 21:40:26.016: E/AndroidRuntime(9076):     at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
12-13 21:40:26.016: E/AndroidRuntime(9076):     at android.content.ContentResolver.query(ContentResolver.java:370)
12-13 21:40:26.016: E/AndroidRuntime(9076):     at android.content.ContentResolver.query(ContentResolver.java:313)
12-13 21:40:26.016: E/AndroidRuntime(9076):     at android.provider.ContactsContract$Contacts.openContactPhotoInputStream(ContactsContract.java:1973)
12-13 21:40:26.016: E/AndroidRuntime(9076):     at android.provider.ContactsContract$Contacts.openContactPhotoInputStream(ContactsContract.java:2004)

请帮忙!

推荐答案

我认为您不应该将照片的 uri 传递给 openContactphotoinputstream.您只需要传递联系人本身的 uri 即可获取位图.

I dont think you should pass the uri of the photo to openContactphotoinputstream. You just need to pass the uri of the contact itself to get the bitmap.

 Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
  InputStream stream = ContactsContract.Contacts.openContactPhotoInputStream(
            mContext.getContentResolver(), uri);

或者,如果您要传递联系人照片 uri,则可以使用

Or if you are going to pass the contact photo uri then you could use

public static Bitmap getContactBitmapFromURI(Context context, Uri uri) {
        InputStream input = context.getContentResolver().openInputStream(uri)
        if (input == null) {
            return null;
        }
        return BitmapFactory.decodeStream(input);
    }

这篇关于将联系人图像加载到位图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆