private string GetContactInfoFromAddress(long address) { var uri = Uri.WithAppendedPath( ContactsContract.PhoneLookup.ContentFilterUri, Uri.Encode(address.ToString())); string[] projection = { ContactsContract.Contacts.InterfaceConsts.Id, ContactsContract.Contacts.InterfaceConsts.DisplayName }; var contactCursor = FetchCursor(uri, projection, null); if (contactCursor.MoveToFirst()) { string name; do { var contactId = contactCursor.GetString( contactCursor.GetColumnIndex(projection[0])); var emailAddress = GetEmailFromContactId(contactId); name = contactCursor.GetString( contactCursor.GetColumnIndex( projection[1])); if (!string.IsNullOrEmpty(emailAddress)) { return($"{name} <{emailAddress}>"); } } while (contactCursor.MoveToNext()); return(name); } else { return(address.ToString()); } }
Bitmap GetPersonPicture(AndroidPerson androidDetails) { if (string.IsNullOrEmpty(androidDetails.LookupID)) { return(null); } Bitmap bmp = null; if (faceCache.TryGet(androidDetails.LookupID, out bmp)) { return(bmp); } var lookupUri = AUri.WithAppendedPath(AUri.WithAppendedPath(ContactsContract.Contacts.ContentLookupUri, androidDetails.LookupID), androidDetails.ContactID); var contactUri = ContactsContract.Contacts.LookupContact(activity.ContentResolver, lookupUri); if (contactUri == null) { return(null); } var photo = ContactsContract.Contacts.OpenContactPhotoInputStream(activity.ContentResolver, contactUri, true); if (photo == null) { return(null); } bmp = Bitmap.CreateScaledBitmap(BitmapFactory.DecodeStream(photo), 64.ToPixels (), 64.ToPixels (), true); faceCache.AddOrUpdate(androidDetails.LookupID, bmp, TimeSpan.FromDays(1)); return(bmp); }
/// <summary> /// Returns ContactInfo for the phone number. /// This may return more than 1 record if they have multiple /// contacts in their contact database that have the same number. /// </summary> /// <param name="phoneNumber">The phone number to look for.</param> /// <param name="context">The context to run the method under.</param> /// <returns>A list of possible contacts.</returns> public static List <ContactInfo> FindContact(string phoneNumber, Context context) { var contactInfo = new List <ContactInfo>(); var uri = Uri.WithAppendedPath( ContactsContract.CommonDataKinds.Phone.ContentFilterUri, Uri.Encode(phoneNumber)); var projection = new[] { ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Id, ContactsContract.CommonDataKinds.Phone.InterfaceConsts.DisplayName }; var cursor = context.ContentResolver.Query(uri, projection, null, null, null, null); try { if (cursor.MoveToFirst()) { do { contactInfo.Add(new ContactInfo { ID = cursor.GetString(cursor.GetColumnIndexOrThrow(projection[0])), Name = cursor.GetString(cursor.GetColumnIndexOrThrow(projection[1])), PhoneNumber = phoneNumber }); } while (cursor.MoveToNext()); } } finally { cursor.Close(); } // Return the contact info that we found. return(contactInfo); }
protected static Uri IdToUri(long id) { var uri = Uri.WithAppendedPath(MediaStore.Images.Media.ExternalContentUri, id.ToString()); return(uri); }
/// <summary> /// Try go get output file /// </summary> /// <param name="context"></param> /// <param name="subdir"></param> /// <param name="name"></param> /// <param name="isPhoto"></param> /// <param name="saveToAlbum"></param> /// <returns></returns> public static Uri GetOutputMediaFile(Context context, string subdir, string name, bool isPhoto, bool saveToAlbum) { subdir = subdir ?? string.Empty; Uri uri; if (string.IsNullOrWhiteSpace(name)) { var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss", CultureInfo.InvariantCulture); if (isPhoto) { name = "IMG_" + timestamp + ".jpg"; } else { name = "VID_" + timestamp + ".mp4"; } } if ((int)Build.VERSION.SdkInt < 29) { var mediaType = (isPhoto) ? Environment.DirectoryPictures : Environment.DirectoryMovies; var directory = saveToAlbum ? Environment.GetExternalStoragePublicDirectory(mediaType) : context.GetExternalFilesDir(mediaType); using (var mediaStorageDir = new Java.IO.File(directory, subdir)) { if (!mediaStorageDir.Exists()) { if (!mediaStorageDir.Mkdirs()) { throw new IOException("Couldn't create directory, have you added the WRITE_EXTERNAL_STORAGE permission?"); } if (!saveToAlbum) { // Ensure this media doesn't show up in gallery apps using (var nomedia = new Java.IO.File(mediaStorageDir, ".nomedia")) nomedia.CreateNewFile(); } } uri = Uri.FromFile(new Java.IO.File(GetUniquePath(mediaStorageDir.Path, name, isPhoto))); } } else { var mediaType = (isPhoto) ? Environment.DirectoryPictures : Environment.DirectoryMovies; var directory = context.GetExternalFilesDir(mediaType); using (var mediaStorageDir = new Java.IO.File(directory, subdir)) { mediaStorageDir.Mkdirs(); if (!saveToAlbum) { // Ensure this media doesn't show up in gallery apps using (var nomedia = new Java.IO.File(mediaStorageDir, ".nomedia")) nomedia.CreateNewFile(); } uri = Uri.FromFile(new Java.IO.File(GetUniquePath(mediaStorageDir.Path, name, isPhoto))); } if (saveToAlbum) { uri = (isPhoto) ? MediaStore.Images.Media.ExternalContentUri : MediaStore.Video.Media.ExternalContentUri; uri = Uri.WithAppendedPath(uri, name); } } return(uri); }