Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I chased my tail around on this for quite a while. The answer from deSelby helped me. Here is my code that downloads images (PNG and JPG) from Facebook, then stores / retrieves the data from a sqlite table using the path as a reference.</p> <p>As you can see, it actually attempts to read the image from the table first, if it is there it simply returns it, otherwise it fetches it from Facebook, stores it in the database and then returns it.</p> <pre><code>private static Drawable fetchPhoto(ContentResolver cr, int source, String path) { Drawable image = null; myDB mDb = myDB.getInstance(); Cursor iCursor = mDb.fetchImage(cr, path); if(iCursor != null &amp;&amp; iCursor.moveToFirst()) { byte[] bb = iCursor.getBlob(iCursor.getColumnIndex(Images.IMAGE)); if(iCursor != null) iCursor.close(); image = new BitmapDrawable(BitmapFactory.decodeByteArray(bb, 0, bb.length)); } else { if(iCursor != null) iCursor.close(); //get image from path try { InputStream is = (InputStream) new URL(path).getContent(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } byte[] barray = baf.toByteArray(); bis.close(); is.close(); /* write image to db */ long id = mDb.writeImage(cr,source,path,barray); image = new BitmapDrawable(BitmapFactory.decodeByteArray(barray, 0, barray.length)); }catch (Exception error) { //System.out.println("Exc="+e); } } return image; } </code></pre> <p>My writeImage looks like this (see below).</p> <p>Images.IMAGE references the name of a BLOB column in my table. The main point here is really that you can write the byte[] directly to the database BLOB column. I write the time (utc) so I can clear old data in accordance with the Facebook api policies on caching data.</p> <pre><code>public long writeImage(ContentResolver contentResolver, int source, String path, byte[] image) { Time now = new Time(); now.setToNow(); ContentValues initialValues = new ContentValues(); initialValues.put(Images.IMAGE, image); initialValues.put(Images.SOURCE, source); initialValues.put(Images.PATH, path); initialValues.put(Images.UTC, now.toMillis(false)); if(source == 0) initialValues.put(Images.DIRTY, 1); // sync user created images Uri newUri = contentResolver.insert(Images.CONTENT_URI, initialValues); String Id = newUri.getPathSegments().get(1); return Long.parseLong(Id); } </code></pre> <p>I use a content provider for my db, but for those who don't the insert code looks like this, which you can use directly into your database without a provider. (FYI only).</p> <pre><code>rowId = db.insert(IMAGES_TABLE, null, values); </code></pre>
 

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