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. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Regular Contributor
key_key
Posts: 59
Registered: ‎01-17-2011
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!

 

 

 

Please use plain text.
Developer
peter_strange
Posts: 17,634
Registered: ‎07-14-2008

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. 

Please use plain text.
New Contributor
kinjalshah_5320
Posts: 9
Registered: ‎07-02-2012
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..

Please use plain text.
Developer
peter_strange
Posts: 17,634
Registered: ‎07-14-2008

Re: How to store Bitmap into SQLite ?

Have you tried BLOB?

Please use plain text.
New Contributor
kinjalshah_5320
Posts: 9
Registered: ‎07-02-2012
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..

Please use plain text.
New Contributor
kinjalshah_5320
Posts: 9
Registered: ‎07-02-2012
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

Please use plain text.