图像未添加到联系人

时间:2016-07-19 18:49:11

标签: android-contacts

以下是我点击通知时用来添加联系人的代码。 除了图像之外,其他所有内容都会添加到联系人中,我缺少什么?

 Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
        ByteArrayOutputStream stream = new ByteArrayOutputStream(bmp.getWidth() * bmp.getHeight());
        bmp.compress(Bitmap.CompressFormat.PNG, 75, stream);
        byte[] byteArray = stream.toByteArray();

        final int notificationId = new Random().nextInt();
        Intent notificationIntent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
        notificationIntent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
        notificationIntent.putExtra(ContactsContract.Intents.Insert.NAME, context.getString(R.string.contact_name));
        notificationIntent.putExtra(ContactsContract.Intents.Insert.PHONE, context.getString(R.string.notification_number));
        notificationIntent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, context.getString(R.string.notification));
        notificationIntent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE, context.getString(R.string.monitor_number));
        notificationIntent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE_TYPE, context.getString(R.string.monitor_name));
        notificationIntent.putExtra(ContactsContract.CommonDataKinds.Photo.PHOTO, byteArray);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.icon)
                .setContentTitle(context.getString(R.string.contact_title))
                .setContentText(context.getString(R.string.contact_message))
                .setSound(defaultSoundUri)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(context.getString(R.string.contact_message)))
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);


 NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = notificationBuilder.build();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(notificationId, notification);

1 个答案:

答案 0 :(得分:0)

我删除了该行

 notificationIntent.setType(ContactsContract.RawContacts.CONTENT_TYPE);

然后添加

 ContentValues row = new ContentValues();
        row.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
        row.put(ContactsContract.CommonDataKinds.Photo.PHOTO, byteArray);
        data.add(row);

Intent notificationIntent = new Intent(ContactsContract.Intents.Insert.ACTION, ContactsContract.Contacts.CONTENT_URI);
notificationIntent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);