13

I'm trying to convert one image from byte[] to Bitmap to show the image in the Android application.

byte[]'s value is got by database and I checked that it was not null. After that, I would like to convert the image but could not success. The program shows that Bitmap's value is null.

I think there are some problems in converting process.

If you know any tips, please show me.

byte[] image = null;
Bitmap bitmap = null;
        try {
            if (rset4 != null) {
                while (rset4.next()) {
                    image = rset4.getBytes("img");
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    bitmap = BitmapFactory.decodeByteArray(image, 0, image.length, options);
                }
            }
            if (bitmap != null) {
                ImageView researcher_img = (ImageView) findViewById(R.id.researcher_img);
                researcher_img.setImageBitmap(bitmap);
                System.out.println("bitmap is not null");
            } else {
                System.out.println("bitmap is null");
            }

        } catch (SQLException e) {

        }

2 Answers 2

24

use below line to convert bytes into Bitmap, it is working for me.

  Bitmap bmp = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);

you need to put above line outside of loop, as it takes Bytes Array and convert into Bitmap.

P.S. :- here imageData is bytes array of Image

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. But it does not work still now. I also use bytes array of image. Is there some problems in my bytes array...?
10

From your code, it seems that you take a portion of the byte array and use the BitmapFactory.decodeByteArray method in that portion. You need to supply the whole byte array in the BitmapFactory.decodeByteArray method.

EDIT from comments

You need to change your select query (or at least know the name (or the index) of the column that has the blob data of the image stored in your db). Also intead of getByte use the getBlob method of the ResultSet class. Let's say that column name is image_data. Having this info, change your code to something like this:

byte[] image = null;
Bitmap bitmap = null;
    try {
        if (rset4 != null) {
                Blob blob = rset4.getBlob("image_data"); //This line gets the image's blob data
                image = blob.getBytes(0, blob.length); //Convert blob to bytearray
                BitmapFactory.Options options = new BitmapFactory.Options();
                bitmap = BitmapFactory.decodeByteArray(image, 0, image.length, options); //Convert bytearray to bitmap
        //for performance free the memmory allocated by the bytearray and the blob variable
        blob.free();
        image = null;
        }
        if (bitmap != null) {
            ImageView researcher_img = (ImageView) findViewById(R.id.researcher_img);
            researcher_img.setImageBitmap(bitmap);
            System.out.println("bitmap is not null");
        } else {
            System.out.println("bitmap is null");
        }

    } catch (SQLException e) {

    }

6 Comments

Thank you for your reply! Please let me know how to supply whole byte array in that method.
Can you please specify what the rset4 variable is? Seeing your posted code, that seems to have your image's byte array.
OK, rset4 is a value of ResultSet to store a result of executing SQL. ResultSet rset4 = null; rset4 = stmt4.executeQuery("select * from images where id = "+ id);
Thank you for your help! I'd like to use getBlob, but unfortunately it occurs an error that notices Type mismatch: cannot convert from Blob to byte[]. So do I need to convert from BLOB into byte array, and also from byte array into Bitmap?
@Benben I have updated my answer again. I apologise, i thought that the getBlob method of the ResultSet class returns byte[] and not Blob.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.