Can not retrieve JPG images in Couchbase mobile 2.0DB21 [Android]

Hi, I am trying to use the Blob API for storing images in couchdb. I followed the docs on https://developer.couchbase.com/documentation/mobile/2.0/guides/couchbase-lite/native-api/attachment/index.html for 2.0DB21 but couldn’t save the image. Then I used a different image (A PNG instead of JPG) and it got saved. I tried with different sizes and everytime JPG images were not getting saved (or maybe problem was with retrieving the imge).

Also in Blob class, getContent() method which should return the byte array is always returning null. getContentStream() is working fine. For JPG images, I was getting an error while converting it to Bitmap using BitmapFactory.

It would be helpful to show what you have done to see if anything is out of place. The documentation there is slightly out of date as it struggles to keep up with the ever changing DB API.

I suspect that it has something to do with the image file that you are trying to save especially size the .png is saved and retrieved fine.

For reference, the following code snippet works for me for jpeg images.

I have a test.jpeg image in my assets folder .

  • To Save Blob
          // load from assets folder
          InputStream inStream = getContext().getAssets().open("test.jpeg");

          Bitmap imageBitmap = BitmapFactory.decodeStream(inStream);

          Bitmap thumbnail = ThumbnailUtils.extractThumbnail(imageBitmap, 150, 150);

          inStream .close();

         // save as attachment to Document . 
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         image.compress(Bitmap.CompressFormat.JPEG, 50, out);
         ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());

         Blob blob = new Blob("image/jpg", in);
         task.setBlob("image", blob);
         try {
             return db.save(task);
         } catch (CouchbaseLiteException e) {
            Log.e(TAG, "Failed to save the doc - %s", e, task);
            //TODO: Error handling
            return null;
        }

  • To Retrieve Blob
        ImageView imageView = convertView.findViewById(R.id.photo);
        Blob thumbnail = task.getBlob("image");
        if (thumbnail != null)
            Glide.with(getContext()).load(thumbnail.getContent()).into(imageView);
        else
            imageView.setImageResource(R.drawable.ic_camera_light);

Thank you for such a quick response. :slight_smile:

I tried with the code snippet that you posted. I am still not able to load JPG images.
Here is the code that I wrote :-

When I try to save compressed thumbnail Bitmap, contentArray is not null but when I try to save full sized image, blob.getContent() returns null.

When I try with PNG image, it works.
PNG file is 1440 X 2560 in resolution (2.7 MB).

I am using two JPG files
640 X 427, 145 KB.
Here is the link to the image that I downloaded.
/https://archive.li/TS69Q/6ba28432d78fc471ba19d41c069190b20a1f9beb.jpg/

Second Image
1920 X 1080 289 KB.
I don’t have link to this file right now.

Tried with this image too, same result.
/https://pbs.twimg.com/profile_images/54789364/JPG-logo-highres.jpg/

The image you shared works for me with the code snippet I shared earlier .
Looked at your gist and except for the fact that its in Kotlin (Yay :-)) , it seems more or less what I have.
I noticed both these lines uncommented though. Can you double check that you had the appropriate line commented out when you used jpeg and png

      thumbnail.compress(Bitmap.CompressFormat.JPEG, 50, out)
       thumbnail.compress(Bitmap.CompressFormat.PNG, 50, out)

Hello @priya.rajagopal , I also have a blob data I am trying to work with. I can actually use getBlob to get the data from server, but if I try to use getContent from the blob, it returns back a null value.

Please, can you tell me where I am getting it all wrong?

Dictionary data = result.getDictionary(1);
** Log.e(“RESULTBLOB”, String.valueOf(data.getValue(“imageFile”)));**
Log.e(“RESULTBLOBCONTENT”, String.valueOf(data.getBlob(“imageFile”).getContent()));

I actually get the expected data on the RESULTBLOB but RESULTBLOBCONTENT actually returns a null value even if I try to user getBlob, I still get a null value.

Moreover, is there a way i can convert my data.getValue to a blob data?

Reference: getContentStream

InputStream is = data.getBlob("imageFile").getContentStream();

Reference: BitmapFactory

Bitmap thumbnail = BitmapFactory.decodeStream(is);

Reference: SetImageBitMap

myview.setImageBitmap(thumbnail);