File tree 6 files changed +74
-4
lines changed
android/src/main/java/getcapacitor/community/contacts
6 files changed +74
-4
lines changed Original file line number Diff line number Diff line change 16
16
import android .provider .ContactsContract .CommonDataKinds .StructuredPostal ;
17
17
import android .provider .ContactsContract .CommonDataKinds .Website ;
18
18
import android .provider .ContactsContract .RawContacts ;
19
+ import android .util .Base64 ;
19
20
import androidx .annotation .NonNull ;
20
21
import androidx .annotation .Nullable ;
21
22
import java .util .ArrayList ;
@@ -313,6 +314,21 @@ public String createContact(CreateContactInput contactInput) {
313
314
ops .add (op .build ());
314
315
}
315
316
317
+ // Image
318
+ if (contactInput .image != null && contactInput .image .base64String != null ) {
319
+ byte [] photoData = Base64 .decode (contactInput .image .base64String , Base64 .DEFAULT );
320
+
321
+ op =
322
+ ContentProviderOperation
323
+ .newInsert (ContactsContract .Data .CONTENT_URI )
324
+ .withValueBackReference (ContactsContract .Data .RAW_CONTACT_ID , 0 )
325
+ // Add to this key
326
+ .withValue (ContactsContract .Data .MIMETYPE , ContactsContract .CommonDataKinds .Photo .CONTENT_ITEM_TYPE )
327
+ // Data
328
+ .withValue (ContactsContract .CommonDataKinds .Photo .PHOTO , photoData );
329
+ ops .add (op .build ());
330
+ }
331
+
316
332
try {
317
333
ContentResolver cr = this .mActivity .getContentResolver ();
318
334
ContentProviderResult [] result = cr .applyBatch (ContactsContract .AUTHORITY , ops );
Original file line number Diff line number Diff line change @@ -45,8 +45,8 @@ public class CreateContactInput {
45
45
// Postal Addresses
46
46
public ArrayList <PostalAddressInput > postalAddresses = new ArrayList <>();
47
47
48
- // @TODO:
49
48
// Image
49
+ public ImageInput image ;
50
50
51
51
CreateContactInput (JSONObject fromJSONObject ) {
52
52
// Name
@@ -153,6 +153,12 @@ public class CreateContactInput {
153
153
}
154
154
}
155
155
}
156
+
157
+ // Image
158
+ JSONObject imageObject = fromJSONObject .optJSONObject ("image" );
159
+ if (imageObject != null ) {
160
+ this .image = new ImageInput (imageObject );
161
+ }
156
162
}
157
163
158
164
public static class PhoneInput {
@@ -215,4 +221,13 @@ public static class PostalAddressInput {
215
221
this .country = fromJSONObject .has ("country" ) ? fromJSONObject .optString ("country" ) : null ;
216
222
}
217
223
}
224
+
225
+ public static class ImageInput {
226
+
227
+ public final String base64String ;
228
+
229
+ ImageInput (JSONObject fromJSONObject ) {
230
+ this .base64String = fromJSONObject .has ("base64String" ) ? fromJSONObject .optString ("base64String" ) : null ;
231
+ }
232
+ }
218
233
}
Original file line number Diff line number Diff line change @@ -266,6 +266,7 @@ pickContact(options: PickContactOptions) => Promise<PickContactResult>
266
266
| ** ` emails ` ** | <code >EmailInput[ ] </code > | Emails |
267
267
| ** ` urls ` ** | <code >string[ ] </code > | URLs |
268
268
| ** ` postalAddresses ` ** | <code >PostalAddressInput[ ] </code > | Postal Addresses |
269
+ | ** ` image ` ** | <code >[ ImageInput] ( #imageinput ) \| null</code > | Image |
269
270
270
271
271
272
#### NameInput
@@ -332,6 +333,13 @@ pickContact(options: PickContactOptions) => Promise<PickContactResult>
332
333
| ** ` country ` ** | <code >string \| null</code > |
333
334
334
335
336
+ #### ImageInput
337
+
338
+ | Prop | Type | Description |
339
+ | ------------------ | --------------------------- | --------------------- |
340
+ | ** ` base64String ` ** | <code >string \| null</code > | Base64 encoded image. |
341
+
342
+
335
343
#### DeleteContactOptions
336
344
337
345
| Prop | Type |
Original file line number Diff line number Diff line change @@ -182,6 +182,15 @@ public class Contacts: NSObject {
182
182
)
183
183
} )
184
184
185
+ // Image
186
+ if let image = contactInput. image {
187
+ if let base64String = image. base64String {
188
+ if let data = Data ( base64Encoded: base64String, options: . ignoreUnknownCharacters) {
189
+ newContact. imageData = data
190
+ }
191
+ }
192
+ }
193
+
185
194
do {
186
195
let cs = CNContactStore ( )
187
196
let saveRequest = CNSaveRequest ( )
Original file line number Diff line number Diff line change @@ -33,8 +33,8 @@ public class CreateContactInput {
33
33
// Postal Addresses
34
34
public var postalAddresses : [ PostalAddressInput ] = [ ]
35
35
36
- // @TODO:
37
36
// Image
37
+ public var image : ImageInput ?
38
38
39
39
init ( _ fromJSONObject: JSObject ) {
40
40
// Name
@@ -103,6 +103,11 @@ public class CreateContactInput {
103
103
}
104
104
}
105
105
}
106
+
107
+ // Image
108
+ if let imageObject = fromJSONObject [ " image " ] as? JSObject {
109
+ self . image = ImageInput . init ( imageObject)
110
+ }
106
111
}
107
112
108
113
static func getType( _ type: String ? , _ fromJSONObject: JSObject ) -> String ? {
@@ -170,4 +175,13 @@ public class CreateContactInput {
170
175
self . country = fromJSONObject [ " country " ] as? String
171
176
}
172
177
}
178
+
179
+ public class ImageInput {
180
+
181
+ public var base64String : String ?
182
+
183
+ init ( _ fromJSONObject: JSObject ) {
184
+ self . base64String = fromJSONObject [ " base64String " ] as? String
185
+ }
186
+ }
173
187
}
Original file line number Diff line number Diff line change @@ -270,6 +270,15 @@ export interface EmailInput {
270
270
address : string | null ;
271
271
}
272
272
273
+ export interface ImageInput {
274
+ /**
275
+ * Base64 encoded image.
276
+ *
277
+ * @example "iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAApgAAAKYB3X3/OAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAANCSURBVEiJtZZPbBtFFMZ/M7ubXdtdb1xSFyeilBapySVU8h8OoFaooFSqiihIVIpQBKci6KEg9Q6H9kovIHoCIVQJJCKE1ENFjnAgcaSGC6rEnxBwA04Tx43t2FnvDAfjkNibxgHxnWb2e/u992bee7tCa00YFsffekFY+nUzFtjW0LrvjRXrCDIAaPLlW0nHL0SsZtVoaF98mLrx3pdhOqLtYPHChahZcYYO7KvPFxvRl5XPp1sN3adWiD1ZAqD6XYK1b/dvE5IWryTt2udLFedwc1+9kLp+vbbpoDh+6TklxBeAi9TL0taeWpdmZzQDry0AcO+jQ12RyohqqoYoo8RDwJrU+qXkjWtfi8Xxt58BdQuwQs9qC/afLwCw8tnQbqYAPsgxE1S6F3EAIXux2oQFKm0ihMsOF71dHYx+f3NND68ghCu1YIoePPQN1pGRABkJ6Bus96CutRZMydTl+TvuiRW1m3n0eDl0vRPcEysqdXn+jsQPsrHMquGeXEaY4Yk4wxWcY5V/9scqOMOVUFthatyTy8QyqwZ+kDURKoMWxNKr2EeqVKcTNOajqKoBgOE28U4tdQl5p5bwCw7BWquaZSzAPlwjlithJtp3pTImSqQRrb2Z8PHGigD4RZuNX6JYj6wj7O4TFLbCO/Mn/m8R+h6rYSUb3ekokRY6f/YukArN979jcW+V/S8g0eT/N3VN3kTqWbQ428m9/8k0P/1aIhF36PccEl6EhOcAUCrXKZXXWS3XKd2vc/TRBG9O5ELC17MmWubD2nKhUKZa26Ba2+D3P+4/MNCFwg59oWVeYhkzgN/JDR8deKBoD7Y+ljEjGZ0sosXVTvbc6RHirr2reNy1OXd6pJsQ+gqjk8VWFYmHrwBzW/n+uMPFiRwHB2I7ih8ciHFxIkd/3Omk5tCDV1t+2nNu5sxxpDFNx+huNhVT3/zMDz8usXC3ddaHBj1GHj/As08fwTS7Kt1HBTmyN29vdwAw+/wbwLVOJ3uAD1wi/dUH7Qei66PfyuRj4Ik9is+hglfbkbfR3cnZm7chlUWLdwmprtCohX4HUtlOcQjLYCu+fzGJH2QRKvP3UNz8bWk1qMxjGTOMThZ3kvgLI5AzFfo379UAAAAASUVORK5CYII="
278
+ */
279
+ base64String ?: string | null ;
280
+ }
281
+
273
282
export interface PostalAddressInput {
274
283
type : PostalAddressType ;
275
284
label ?: string | null ;
@@ -327,8 +336,7 @@ export interface ContactInput {
327
336
/**
328
337
* Image
329
338
*/
330
- // @TODO :
331
- // image?: ImageInput | null;
339
+ image ?: ImageInput | null ;
332
340
}
333
341
334
342
export interface CreateContactResult {
You can’t perform that action at this time.
0 commit comments