Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to store Image as blob in Sqlite & how to retrieve it?
    primarykey
    data
    text
    <p>I want to store an image(from url) into a sqlite database.</p> <p>For that I use: </p> <pre><code>db = new DataBase(getApplicationContext()); URL url = new URL("http://sree.cc/wp-content/uploads/schogini_team.png"); URLConnection ucon = url.openConnection(); InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is,128); ByteArrayBuffer barb= new ByteArrayBuffer(128); int current = 0; while ((current = bis.read()) != -1) { barb.append((byte) current); } ContentValues filedata= new ContentValues(); filedata.put(DataBase.IMG_SRC,barb.toByteArray()); db.insert(DataBase.Table_Img, null, filedata); </code></pre> <p>In the <code>Insert()</code>:</p> <pre><code>public void insert(String tableImg, Object object, ContentValues dataToInsert) { // TODO Auto-generated method stub String sql = "INSERT INTO "+tableImg+" ("+ID+","+IMG_SRC+") " + "VALUES ('"+1+"','"+dataToInsert+"')"; db.execSQL(sql); } </code></pre> <p>For the retrieval of image:</p> <pre><code>Cursor cursor = db.selectDataToShow(DataBase.Table_Img, DataBase.IMG_SRC); byte[] imageByteArray=cursor.getBlob(cursor.getColumnIndex(DataBase.IMG_SRC)); cursor.close(); ByteArrayInputStream imageStream = new ByteArrayInputStream(imageByteArray); Bitmap theImage = BitmapFactory.decodeStream(imageStream); System.out.println("&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; "+theImage); </code></pre> <p>So here I got <code>null</code>.</p> <p>And in my database the value of image stored as: <code>Image=[B@43e5ac48]</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload