public void writeDisplayPhoto(long rawContactId, byte[] photo) { 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. } }
public void writeDisplayPhoto(long rawContactId, byte[] photo) { 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. } }
public void writeDisplayPhoto(long rawContactId, byte[] photo) { 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. } }
/** @return true if picture was changed false otherwise. */ public boolean setDisplayPhotoByRawContactId(long rawContactId, Bitmap bmp) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] byteArray = stream.toByteArray(); Uri pictureUri = Uri.withAppendedPath(ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId), RawContacts.DisplayPhoto.CONTENT_DIRECTORY); try { AssetFileDescriptor afd = getContentResolver().openAssetFileDescriptor(pictureUri, "rw"); OutputStream os = afd.createOutputStream(); os.write(byteArray); os.close(); afd.close(); return true; } catch (IOException e) { e.printStackTrace(); } return false; }
private static void overwriteHiResPhoto( final ContentResolver contentResolver, final Uri contactUri, final Bitmap hiResBitmap ) { final Uri displayPhotoUri; displayPhotoUri = Uri.withAppendedPath( contactUri, ContactsContract.Contacts.Photo.DISPLAY_PHOTO ); AssetFileDescriptor descriptor = null; try { descriptor = contentResolver.openAssetFileDescriptor(displayPhotoUri, "w"); } catch (IOException e) { e.printStackTrace(); } if (descriptor != null) { OutputStream os; try { os = descriptor.createOutputStream(); hiResBitmap.compress(Bitmap.CompressFormat.PNG, 0, os); os.close(); } catch (IOException e) { e.printStackTrace(); } } }