Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Regular Contributor
Posts: 59
Registered: ‎01-17-2011
My Device: 8520
My Carrier: Vodafone

How to store Bitmap into SQLite ?

Hello friends,

 

I want to store a bitmap into SQLite database.

query for that  create a table structure as follow.

 

"CREATE TABLE card_table  ('card_id' INTEGER PRIMARY KEY,'picture'  BLOB)"

 

for storing a Bitmap convert bitmap into byte array and then i store

so it gives an exception out of memory.

 

But i am not able to store a Bitmap into table.

 

Please help me!

 

 

 

Developer
Posts: 19,618
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to store Bitmap into SQLite ?

I am not familiar with storing BLOBS in SQL, but if you wanted to convert the Bitmap to bytes yourself, you could use something like JPEGEncodedImage.encode(..) to do it, then do a getData to get the bytes. 

New Contributor
Posts: 9
Registered: ‎07-02-2012
My Device: blackberry 9930,9900
My Carrier: Blackberry

Re: How to store Bitmap into SQLite ?

Hello.

 

I also want to store Image in Database.. so what i have to do?? plz help me..

Developer
Posts: 19,618
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to store Bitmap into SQLite ?

Have you tried BLOB?

New Contributor
Posts: 9
Registered: ‎07-02-2012
My Device: blackberry 9930,9900
My Carrier: Blackberry

Re: How to store Bitmap into SQLite ?

Yes, i tried it.. and i pass byte[ ] in insert query.. bt it will not work..

 

 

 d = DatabaseFactory.open(uri);
            Statement st = d.createStatement(
                "INSERT INTO img(Name,picture) " +
                "VALUES ('"+name+"','"+pic+"')");
              
           
            st.prepare();
            st.bind(1, name);
            st.bind(2, pic);
            st.execute();
           st.close();
            d.close();

 

 

bt data is not inserted in database..

New Contributor
Posts: 9
Registered: ‎07-02-2012
My Device: blackberry 9930,9900
My Carrier: Blackberry

Re: How to store Bitmap into SQLite ?

Hey I got solution..and its work perfectly fine..

For inserting,

 

EncodedImage pic = getImage();
byte
[] imageBytes = pic.getData();
Statement st
= db.createStatement("INSERT INTO Image (image_data) values (?)");
st
.prepare();
st
.bind(1, imageBytes);
st
.execute();

 

And for Fetching,

 

Statement st = db.createStatement("SELECT image_data FROM Image");
st
.prepare();
Cursor c = st.getCursor();
Row r
;
while(c.next()){
   r
= c.getRow();
   byte
[] pic=r.getBlobBytes(0);
   EncodedImage _bmap
= EncodedImage.createEncodedImage(pic,0, pic.length);
   BitmapField bmf
=new BitmapField(_bmap.getBitmap());
   
add(bmf);
}
st
.close();  // moved this line out of the loop

Contributor
Posts: 24
Registered: ‎01-06-2014
My Device: 9220
My Carrier: 9220

Re: How to store Bitmap into SQLite ?

what is getImage method?

Developer
Posts: 16,625
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to store Bitmap into SQLite ?

obviously a custom method that returns an EncodedImage. If you want to add an image to a database you usually start with an image...
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 24
Registered: ‎01-06-2014
My Device: 9220
My Carrier: 9220

Re: How to store Bitmap into SQLite ?

 
Contributor
Posts: 24
Registered: ‎01-06-2014
My Device: 9220
My Carrier: 9220

Re: How to store Bitmap into SQLite ?

It's storing as null value. Can u explain how to store the image in sqlite DB?