How to put a BLOB in SQLite?

Put your problem here if it does not fit any of the other categories.

How to put a BLOB in SQLite?

Postby snowtiger » Thu Feb 21, 2008 3:48 pm

:?:

Has anybody an idea, how to add an BLOB to the Android SQLite database?

I can see only methods for simple filetypes: String, Int, Double, Byte,...
In my case, i want to put a drawable or bitmap to my database. It starts like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. [...]ContentValues values = new ContentValues();
  2.  
  3.  
  4.  
  5.                 values.put(Scheme.Images.IMAGE_ID, imageId);
  6.  
  7.        
  8.  
  9.                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
  10.  
  11.         ObjectOutputStream out;
  12.  
  13.                 try {
  14.  
  15.                         out = new ObjectOutputStream(bos);
  16.  
  17.                         out.writeObject(myImage);
  18.  
  19.                 } catch (IOException e) {
  20.  
  21.                         e.printStackTrace();
  22.  
  23.                 }
  24.  
  25.        
  26.  
  27.                 values.put(Scheme.Images.THUMB_DATA, bos.toByteArray());
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
But the last line doesn't work, only byte is accepted, no byteArray :(
snowtiger
Developer
Developer
 
Posts: 34
Joined: Fri Dec 21, 2007 10:44 am

Top

Postby plusminus » Thu Feb 21, 2008 4:10 pm

Hello snowtiger,

BLOB is not supported and probably wont be implemented in future.

You could do that using a content provider, like Google suggests :arrow: http://code.google.com/android/devel/da ... ntprovider :
# If you are exposing byte data, such as a bitmap file, the field that stores this data should actually be a string field with a content:// URI for that specific file. This is the field that clients will call to retrieve this data. The content provider for that content type (it can be the same content provider or another content provider — for example, if you're storing a photo you would use the media content provider) should implement a field named _data for that record. The _data field lists the exact file path on the device for that file. This field is not intended to be read by the client, but by the ContentResolver. The client will call ContentResolver.openOutputStream() on the user-facing field holding the URI for the item (for example, the column named photo might have a value content://media/images/4453). The ContentResolver will request the _data field for that record, and because it has higher permissions than a client, it should be able to access that file directly and return a read wrapper for that file to the client.


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby snowtiger » Thu Feb 21, 2008 4:57 pm

Thanx for ur reply +-,

I am already using a content provider. The problem is, that I want to do some P2P stuff in future and exchange images between users. So it would be much easier to have one database, where I can put in my imageId, tags, desription and a small thumbnail.
Besides I don't really like the google pattern to have thousands of files in one directory. If I use the insertImage-method for my current testset of 300 images, I get a new directory on my sdcard with 900 files/images. I would prefer the 300 images on my sdcard and 1 database with all thumbnails.

Well maybe I will have a look at db4o...
snowtiger
Developer
Developer
 
Posts: 34
Joined: Fri Dec 21, 2007 10:44 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 15 guests