Insert and Read Attachment. Couchbase Lite Android Mobile

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”.

Link: http://developer.couchbase.com/documentation/mobile/1.2/develop/training/build-first-android-app/do-crud/index.html

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);

    }
}

I’m not sure what you’re asking. Is the code not working? If not, what’s going wrong?

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 ?

Best Regards,
Harris

Sure, just call setAttachment multiple times. Each attachment just has to have a different name.

Is this correct?

        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();

private void readAttachment(Database database) {
Document doc = database.getDocument(“1”);
Revision rev = doc.getCurrentRevision();
Attachment att = rev.getAttachment(“image”);
Attachment att1 = rev.getAttachment(“image1”);
try {
if (att != null) {
InputStream is = att.getContent();
InputStream is1 = att1.getContent();
Drawable d = Drawable.createFromStream(is, “res”);
Drawable d1 = Drawable.createFromStream(is1, “res”);

                image.setImageDrawable(d);
                image2.setImageDrawable(d1);
            }

        } catch (Exception e) {
            Log.e(TAG, "HELLO WORLD", e);

        }
    }

Sure; I don’t know the Java API very well, but the attachment code looks correct.

Thanks Jens for your helpful advice.

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)

It’s a conflict. Read about update conflicts here.

Thank you Jens,

May i know what is this “SRC name”?

Drawable d = Drawable.createFromStream(is, "src name");

I have no idea what a Drawable is; I’m not an Android developer. It’s not part of our API. Sorry.

Alright Thank you once again :slight_smile: