I would like to insert an image into ultralitej-db (Android) and later get it from db to display it again.

I'm able to insert and select it from db but the displayed image is all black.


ps = mConn.prepareStatement(query);
        OutputStream os = ps.getBlobOutputStream(1);


        success = ps.execute();


                            InputStream is = rs.getBlobInputStream("Picture");
                            ByteArrayOutputStream buffer = new ByteArrayOutputStream();

                            int nRead;
                            byte[] bytes = new byte[16384];

                            try {
                                while ((nRead = is.read(bytes, 0, bytes.length)) != -1) {
                                  buffer.write(bytes, 0, nRead);


                            } catch (IOException e) {

UPDATE it seems that the insert is wrong. i selected other images which are already stored in the db. they are displayed correctly.

asked 11 Jun '14, 04:57

Teddy's gravatar image

accept rate: 100%

edited 12 Aug '14, 13:22

PhilippeBertrand%20_SAP_'s gravatar image


Hi - can you post the SQLA Version you are using

select @@version;

will find it.

(11 Jun '14, 07:41) Justin Willey
Replies hidden

i'm using SQLA 16.0

(11 Jun '14, 08:23) Teddy

Is this ULj for BlackBerry or Android - I ask because Android uses JNI to access the native UL runtime code.

(11 Jun '14, 09:43) Chris Keating

I ran a quick test on Android in which I retrieve a good image from the database and write it back into a new row. That image is set correctly. This is based on your code where the image is set in the prepared statement update as:

 os.write( buffer.toByteArray());

I guess the question is is "photo" a good value when you write it to the output stream?

(11 Jun '14, 14:20) Chris Keating
Replies hidden

photo is an byteArray of the Image. The code to obtain the byteArray from the image is:

ByteArrayOutputStream bos = new ByteArrayOutputStream(); mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);

byte[] bArray = bos.toByteArray();

(13 Jun '14, 04:16) Teddy

Have you verified that the bos after you you loaded the file and compressed the bitmap looks correct before storing it to the database. The database will only store the information that it is provided. I have modified by test application to implement the Bitmap.compress() and assign the ByteArrayOutputStream's byte array into the prepared statement. I am not seeing any problems with the image in my tests.

(16 Jun '14, 09:42) Chris Keating
showing 4 of 6 show all flat view

better late than never. i found the solution for my problem. it was a converting/format thing with jpeg.

i used mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); and got the whole black image. then i tried mBitmap.compress(Bitmap.CompressFormat.PNG, 100, bos); and it works.

thanks to all who gave me advices to solve the problem

permanent link

answered 11 Aug '14, 04:42

Teddy's gravatar image

accept rate: 100%

edited 17 Sep '14, 06:47

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:


question asked: 11 Jun '14, 04:57

question was seen: 1,839 times

last updated: 17 Sep '14, 06:47