Problem of creating Drawable from SQLite Blob

I am caching image files as blob in SQLite database. I have a similar application on another platform that does the same with the same image files. Databases on both platforms report the same size for the same images. Therefore, I think, but can not guarantee that the image data gets into the database without changes.

But when I try to create a Drawable, the console prints "DEBUG / skia (267): --- decoder-> decode returns false".

Steps:

  • Read blob into byte array.

    byte [] b = new byte [imageDataCursor.getInt (0)];

    b = imageDataCursor.getBlob (1);

  • Create an InputStream from an array of bytes.

    ByteArrayInputStream = new ByteArrayInputStream (b);

  • Create a Drawable from an InputStream. (this is what creates the “decoder” message above)

    Drawable drw = Drawable.createFromStream (is, "articleImage");

  • Set ImageView.image to Drawable.

    imgView.setImageDrawable (DRW);

There is no joy so far. Any suggestions?

+5
source share
2 answers

I will post my solution to help anyone who has a similar problem.

I tried to check the byte array that I am reading from the database, writing it to a file, and then viewing the file. It turns out that the blob data in the database is not complete - I get a few lines of the image, but not the whole image.

BufferedInputStream ByteArrayBuffer . sqlite :

             try {
             HttpGet getMethod=new HttpGet(url);
                HttpClient client=new DefaultHttpClient();
             HttpResponse response = client.execute(getMethod);
             HttpEntity entity = response.getEntity();
             InputStream in = entity.getContent();
             BufferedInputStream bis = new BufferedInputStream(in);
             ByteArrayBuffer baf = new ByteArrayBuffer(50);
             int current = 0;
             while ((current = bis.read()) != -1) {
              baf.append((byte) current);
             }
             byte[] b = baf.toByteArray();
           database.updateArticleWithImageData(b, imageKey);
         }
         catch (Throwable t) {
          android.util.Log.e("MyApp", "Exception fetching image file", t);
         }
+1

. deSelby . , (PNG JPG) Facebook, / sqlite, .

, , , , Facebook, .

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 && 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;
}

My writeImage (. ).

Images.IMAGE BLOB . , [] BLOB . (utc), Facebook api .

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);

}

db, , , . ( FYI).

rowId = db.insert(IMAGES_TABLE, null, values);
0

All Articles