Attach a Photo
In this document
A user can associate a photo with each task item, for instance a picture of the right brand of hot-sauce. We add the attachments to a new revision of the document which contains a reference with some metadata about the attachment.
Take a Photo
Before we can store the attachment, we have to get access to the data. This can be via the camera, or loaded from the user's library.
#pragma mark - UIImagePickerViewDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *selectedImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage];
if (taskToAddImageTo) {
[taskToAddImageTo setImage:[self dataForImage:selectedImage] contentType:ImageDataContentType];
NSError *error;
if (![taskToAddImageTo save:&error]) {
//TODO: Show an error dialog
}
} else {
// new task
imageForNewTask = selectedImage;
[self updateAddImageButtonWithImage:imageForNewTask];
}
if (imagePickerPopover) {
[imagePickerPopover dismissPopoverAnimated:YES];
} else {
[picker.presentingViewController dismissViewControllerAnimated:YES completion:^{ }];
}
}
No code example is currently available.No code example is currently available.@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) {
if (mCurrentTaskToAttachImage != null) {
mCurrentTaskToAttachImage = null;
}
return;
}
if (requestCode == REQUEST_TAKE_PHOTO) {
mImageToBeAttached = BitmapFactory.decodeFile(mImagePathToBeAttached);
// Delete the temporary image file
File file = new File(mImagePathToBeAttached);
file.delete();
mImagePathToBeAttached = null;
} else if (requestCode == REQUEST_CHOOSE_PHOTO) {
try {
Uri uri = data.getData();
mImageToBeAttached = MediaStore.Images.Media.getBitmap(
getActivity().getContentResolver(), uri);
} catch (IOException e) {
Log.e(Application.TAG, "Cannot get a selected photo from the gallery.", e);
}
}
if (mImageToBeAttached != null) {
if (mCurrentTaskToAttachImage != null) {
try {
Task.attachImage(mCurrentTaskToAttachImage, mImageToBeAttached);
} catch (CouchbaseLiteException e) {
Log.e(Application.TAG, "Cannot attach an image to a task.", e);
}
} else { // Attach an image for a new task
Bitmap thumbnail = ThumbnailUtils.extractThumbnail(
mImageToBeAttached, THUMBNAIL_SIZE_PX, THUMBNAIL_SIZE_PX);
ImageView imageView = (ImageView) getActivity().findViewById(R.id.image);
imageView.setImageBitmap(thumbnail);
}
}
// Ensure resetting the task to attach an image
if (mCurrentTaskToAttachImage != null) {
mCurrentTaskToAttachImage = null;
}
}
No code example is currently available.
Save it to the Database
Once we have the picture we save it to the database.
- (void) setImage: (NSData*)image contentType: (NSString*)contentType {
[self setAttachmentNamed:kTaskImageName withContentType:contentType content:image];
}
No code example is currently available.public static void attachImage(Document task, Bitmap image) throws CouchbaseLiteException {
if (task == null || image == null) return;
UnsavedRevision revision = task.createRevision();
ByteArrayOutputStream out = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 50, out);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
revision.setAttachment("image", "image/jpg", in);
revision.save();
}
public static void attachImage(Document task, Bitmap image) throws CouchbaseLiteException {
if (task == null || image == null) return;
UnsavedRevision revision = task.createRevision();
ByteArrayOutputStream out = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 50, out);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
revision.setAttachment("image", "image/jpg", in);
revision.save();
}
No code example is currently available.