开发者

Retrieving BLOB from SQLite database android

I have a database with some images inside, saved as BLOB. I didn't have any problem with the database until now, I try to retrieve the BLOB as I would do with any other data but it's not working:

Cursor c = mDb.query(" ... ");
c.moveToFirst();

ByteArrayInputStream inputStream = new ByteArrayInputStream(c.getBlob(0));
Bitmap b = BitmapFactory.decodeStream(inputStream);

The problem is that I get an exception when the program is trying to get the information of "c.getBlob(0)". It's also not working for "byte[] b = c.getBlob(0)".开发者_如何学编程

Possible reasons:

  • The file is too large? (1.2Mb)
  • I have to do something else to configure the kind of BLOB that it is?

Thanks!


SOLVED: when I tried accidentally with other file it worked! Then I realized that the file was smaller (less than 1Mb), so the problem seems to be the size.

Now my question is, can I configure it to make the BLOB support large files?

Thanks!


Did you verify that there is actually data in your cursor before attempting to retrieve anything?

if(c.getCount() > 0){do something}

if you Don't do this then you will be attempting to retrieve something that is already null which is a BIG NO NO

Also when doing your get of the elements it NOT good practice to retrieve like this: c.getBlob(0)

Other info

The reason for this is because if you decide to change your columns and change the order, this will heavily bite you.

My suggestion is to have static variables that exist in a helper class that reference the column names you have defined such like this:

c.getString(c.getColumnIndex(String columnName))

This is far better practice as you can easily change the database by just referencing the name.

Read my post here on how to handle a database and make indirect calls to your database. You will be thankful of all the headaches you will avoid.

Check here: Storing Lists to A Database, and Retrieving Them All Together : Android

Also when posting a question, a logcat of the error helps solve the problem faster.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜