Note that there are some explanatory texts on larger screens.

plurals
  1. POORMLite and Images saved as BLOB on Android
    text
    copied!<p>So I recently switched my database stuff over to ORMLite in my android tablet application I am writing. So far so good, got most things refactored/recoded. Though I am having issues to what was originally stored in the database as a BLOB. In my original data model it looked like this:</p> <pre><code>byte[] imageBytes; </code></pre> <p>but I don't think I can use that in ORMLite, best I could tell it has to be a string so now I have:</p> <pre><code>@DatabaseField String picture; </code></pre> <p>But now, I am confused as to how to read and write those data bits as bytes, etc...I was using code like this to ferry data to and from the database:</p> <pre><code>... //Set the clients image to what was captured... Bitmap resized2= android.graphics.Bitmap.createScaledBitmap(thumbnail, thumbnail.getWidth()/2, thumbnail.getHeight()/2, true); ByteArrayOutputStream baos = new ByteArrayOutputStream(); resized2.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object byte[] b = baos.toByteArray(); mClient.setImageBytes(b); myImage.setImageBitmap(resized2); //do save to the database of the image to the blob whose column is picture ContentValues initialValues = new ContentValues(); initialValues.put("picture", mClient.getImageBytes()); String [] strArray = {""+sid}; long n = dbAdapter.updateRecordsInDB("clients", initialValues, "_id=?", strArray); </code></pre> <p>So now thats how I WAS saving the image, I am not sure how to do this if there are no BLOBS in the ORMLite and I have to use strings?</p> <p>for completeness this is how i would display the image:</p> <pre><code>if(mClient.getImageBytes().length &lt;= 1) //no image in database, thats fine use generic myImage.setImageResource(R.drawable.sillo); // create a sillouhtette face icon here... else myImage.setImageBitmap((BitmapFactory.decodeByteArray(mClient.getImageBytes(),0, (mClient.getImageBytes()).length))); </code></pre> <p>So what do i have to do to get these images in and out of the database, and is the field type of String correct for a "Blob" ?</p>
 

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