Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将照片添加到联系人_Android_Image_Contacts - Fatal编程技术网

Android 将照片添加到联系人

Android 将照片添加到联系人,android,image,contacts,Android,Image,Contacts,我正在尝试将联系人添加到电话通讯簿。 我成功了:我添加了一个新联系人并为其分配了一个手机号码 现在我需要将我资源目录中的JPG作为联系人照片添加到联系人中。 我正在找教程,但找不到 我需要针对旧手机,所以我需要使用旧联系人API 有人能帮忙吗 ContentValues contact = new ContentValues(); contact.put(People.NAME, "testContact"); Uri insertUri = activity.getContentResolv

我正在尝试将联系人添加到电话通讯簿。
我成功了:我添加了一个新联系人并为其分配了一个手机号码

现在我需要将我资源目录中的JPG作为联系人照片添加到联系人中。
我正在找教程,但找不到

我需要针对旧手机,所以我需要使用旧联系人API

有人能帮忙吗

ContentValues contact = new ContentValues();

contact.put(People.NAME, "testContact");
Uri insertUri = activity.getContentResolver().insert(People.CONTENT_URI, contact);

Uri phoneUri = Uri.withAppendedPath(insertUri, People.Phones.CONTENT_DIRECTORY);
contact.clear();
contact.put(People.Phones.TYPE, People.TYPE_MOBILE);
contact.put(People.NUMBER, "12128911");


updateUri = activity.getContentResolver().insert(phoneUri, contact);

我使用新的APi 8+,您也可以使用它(为了支持清单文件中的较低版本,请使用minSDKVersion what You want.)

我所做的是,(我使用的是.PNG格式的位图)


请尝试此代码可能对您有所帮助。

这是使用ContactsContract类,旧手机(<2.1)不提供这些类。是的,我已经提到过,它来自8+,这就是为什么我告诉您使用此代码,并在清单中输入您想要的版本。对不起,我是Android新手,这对运行Android 1.6、2.1等的手机有效吗?我认为这些库是在更高版本之前才引入的。是的,你可以使用它,在android中,所有新版本都支持旧版本,所以只需使用API 8制作一个应用程序。另外,看看android应用程序的兼容模式,了解如何支持旧版本。我认为您必须在清单文件中提到minSDKVersion标记。
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG , 75, stream);

operations.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
           .withValue(ContactsContract.Data.RAW_CONTACT_ID, 9) // here 9 is _ID where I'm inserting image
           .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
           .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
           .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO,stream.toByteArray())
           .build());

    try {
         stream.flush();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
final Uri uri = ContactsContract.Contacts.CONTENT_URI;
    final String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.PHOTO_URI
    };
    //boolean mShowInvisible = false;
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
    String[] selectionArgs = null;
    final String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    m_curContacts = managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    String[] fields = new String[] {ContactsContract.Data.DISPLAY_NAME, ContactsContract.Contacts.PHOTO_URI};
    myadapter= new MySimpleCursorAdapter(this, R.layout.list_search, m_curContacts, fields, new int []{R.id.textView1,R.id.imageView1});