Programmatically change contact image

I have an image that is stored on an Android phone. I want to be able to change the image of a contact.

What I have done so far is to start the contact picker, select the user, and then get the URI of the selected contact. From this contact, I can get the associated rawContact, and I use this code .

Uri rawContactPhotoUri = Uri.withAppendedPath( ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId), RawContacts.DisplayPhoto.CONTENT_DIRECTORY); try { AssetFileDescriptor fd = getContentResolver().openAssetFileDescriptor(rawContactPhotoUri, "rw"); OutputStream os = fd.createOutputStream(); os.write(photo); os.close(); fd.close(); } catch (IOException e) { // Handle error cases. } 

The problem is that AssetFIleDescriptor is always empty (when I find the length on it, we always get -1).

I am not asking for the whole solution, just some of them lead to something that can help me with this. I cannot find this problem already on StackOverflow, so any help would be appreciated.

EDIT

Whenever we ask a question, we always find a solution. I want to share it with others.

So, I refused the link to android and found another one: http://wptrafficanalyzer.in/blog/programatically-adding-contacts-with-photo-using-contacts-provider-in-android-example/

The image picker returns the Uri of the selected contact, so with this you can get Contact._ID:

 // This is onActivityResult final Uri uri = data.getData(); final Cursor cursor1 = getContentResolver().query(uri, null, null, null, null); cursor.moveToFirst(); final long contactId = cursor1.getLong(cursor1.getColumnIndex(Contacts._ID); cursor1.close(); 

Then I had to get RawContactId:

 final Cursor cursor2 = getContentResolver().query(RawContacts.CONTENT_URI, null, RawContacts.Contact_ID + "=?", new String[] {String.valueOf(contactId)}, null); cursor2.moveToFirst(); final long rawContactId = cursor2.getLong(cursor2.getColumnIndex(RawContacts._ID)); cursor2.close(); 

Then I had to get Data._ID from RawContacts (same as above).

Then I used ContentProviderOperations:

 final ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) .withSelection(Data._ID, dataId), .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, byteArrayOfThePicture); getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 

And it works like a charm. Hope this helps.

+4
source share
1 answer
 String contactId ="10001"; // change it as your IDs if (mBitmap != null) { // Picture try { ByteArrayOutputStream image = new ByteArrayOutputStream(); mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, image); Uri rawContactUri = null; Cursor rawContactCursor = managedQuery( ContactsContract.RawContacts.CONTENT_URI, new String[]{ContactsContract.RawContacts._ID}, ContactsContract.RawContacts.CONTACT_ID + " = " + contactId, null, null); if (!rawContactCursor.isAfterLast()) { rawContactCursor.moveToFirst(); rawContactUri = ContactsContract.RawContacts.CONTENT_URI.buildUpon().appendPath("" + rawContactCursor.getLong(0)).build(); } rawContactCursor.close(); ContentValues values = new ContentValues(); int photoRow = -1; String where111 = ContactsContract.Data.RAW_CONTACT_ID + " == " + ContentUris.parseId(rawContactUri) + " AND " + ContactsContract.Data.MIMETYPE + "=='" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'"; Cursor cursor = managedQuery( ContactsContract.Data.CONTENT_URI, null, where111, null, null); int idIdx = cursor.getColumnIndexOrThrow(ContactsContract.Data._ID); if (cursor.moveToFirst()) { photoRow = cursor.getInt(idIdx); } cursor.close(); values.put(ContactsContract.Data.RAW_CONTACT_ID, ContentUris.parseId(rawContactUri)); values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1); values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray()); values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE); if (photoRow >= 0) { getContentResolver().update( ContactsContract.Data.CONTENT_URI, values, ContactsContract.Data._ID + " = " + photoRow, null); } else { getContentResolver().insert( ContactsContract.Data.CONTENT_URI, values); } } catch (Exception e) { Log.e(" !_@ @Image_Exception", e + ""); } } try { getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); } catch (Exception e) { Log.e("@@@@@UPLOADERR", e + ""); } 
+1
source

Source: https://habr.com/ru/post/1492720/


All Articles