byte[] to image android
Solution 1
Use BitmapFactory.decodeByteArray() method:
byte[] blob=c.getBlob("yourcolumnname");
Bitmap bmp=BitmapFactory.decodeByteArray(blob,0,blob.length);
ImageView image=new ImageView(this);
image.setImageBitmap(bmp);
Look at this thread too.
Solution 2
I prefer to convert the array of bytes to Drawable directly. It is the best format to use in Android. Bitmaps generated leaks in the past.
Drawable d = Drawable.createFromStream(new ByteArrayInputStream(ARRAY_BYTES), null);
Solution 3
Use BitmapFactory.decodeByteArray().
Sephy
Free s/Lance/Spirit Frontender with a touch of backend culture (Fullstack :D ? ) Enjoys playing with all the crazy stuff going about the web platform (React, React Native, new Web APIs, Angular, NodeJs ...)
Updated on July 09, 2022Comments
-
Sephy almost 2 years
My issue is as follows : I have stored a few pictures into the sqlite database, using the blob format, which seems to work ok. now i want to get my pictures out of the DB and put then back into images... to complicate the matter, their format is variable (png, jpg, maybe something else, im not sure) Is there a way of doing so in android?
thank you
-
Sephy about 14 yearsthx for the hint, i'll have a closer look and come back to you if I don't find.
-
Sephy about 14 yearsthat's exactly what i did, but i was using drawable so I added a cast, but i don't think i need it. thanks for the help guys. thanks for the detailed explanation here
-
systempuntoout about 14 yearsdecodeByteArray method return null if the image could not be decoded; check your code that store to db.
-
Toshe about 12 yearsthis doesn't work on motorola! decodeByteArray says that I should send valid data it is not image data that I am sending. why? how to fix it?