Hi Guys, i am not sure whether i am doing it properly. I want to attach a picture from drawable to my document . I followed the tutorial in this link under “Adding an Attachment”.
I am not sure whether i am doing it right as i am new to couchbase lite. The 1st method is where i add the attachment and the 2nd method is where i want to display the attachment in an image view.
private void addAttachment(Database database) {
Document document = database.getDocument("1");
try {
/* Add an attachment with sample data as POC */
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.smoke);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitMapData = stream.toByteArray();
ByteArrayInputStream inputStream = new ByteArrayInputStream(bitMapData);
UnsavedRevision revision = document.getCurrentRevision().createRevision();
revision.setAttachment("binaryData", "image/jpeg", inputStream);
/* Save doc & attachment to the local DB */
revision.save();
} catch (CouchbaseLiteException e) {
Log.e(TAG, "HELLO", e);
}
}
private void readAttachment(Database database) {
Document doc = database.getDocument("1");
Revision rev = doc.getCurrentRevision();
Attachment att = rev.getAttachment("smoke.png");
try{
if (att != null) {
InputStream is = att.getContent();
Drawable d = Drawable.createFromStream(is, "res");
image.setImageDrawable(d);
}
}catch(Exception e)
{
Log.e(TAG, "HELLO WORLD", e);
}
}
Hi Jens, Thanks for the reply. My goal of my program is to display images from couchbase lite database. My initial problem was that with the code i shown above, it was not displaying the image in the android emulator.
So i went by storing the image into drawable first. Then retrieve it from the drawable to store into the couchbase lite database. Then retrieve the image from the couchbase lite database and display it on an image view.
I realized my problem i did above was just a naming convention.
Attachment att = rev.getAttachment(“smoke.png”);
change to
Attachment att = rev.getAttachment(“binarydata”);
Howerver i have another question for you,
is it possible to store 2 attachment into a single doucument ? and how ?
UnsavedRevision revision = document.getCurrentRevision().createRevision();
revision.setAttachment("image", "image/png", inputStream); //image is the name of caegory
revision.setAttachment("image1", "image/png", inputStream1); //image is the name of caegory
revision.save();
i keep getting this error when i insert data into my document? Could you educate me into what i should do when i see this warning ?
com.couchbase.lite.CouchbaseLiteException, Status: 409 (HTTP 409 conflict)