Getting BLOBs from SQLite Database

I have a database with some images inside, saved as a BLOB. I have not had any problems with the database so far, I am trying to get a BLOB, as I would with any other data, but it does not work:

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 tries to get information about "c.getBlob (0)". It also does not work for "byte [] b = c.getBlob (0)".

Possible reasons:

  • Is the file too large? (1.2Mb)
  • Do I need to do something else to configure the type of BLOB that it is?

Thanks!


SOLVED: when I tried accidentally with another file, it works! Then I realized that the file is smaller (less than 1 MB), so the problem seems to be size.

Now my question is: can I configure it so that BLOB supports large files?

Thanks!

+2
source share
1 answer

Did you check that there really is data in the cursor before trying to extract anything?

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

if you Do not , then you will try to get what is already null, which is BIG NO NO

Also, when you get your items, NOT to get this information: c.getBlob(0)

Other information

The reason for this is that if you decide to change your columns and reorder, it will bite you a lot.

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

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

This is much better since you can easily modify the database simply by referencing the name.

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

Check here: Saving lists to a database and extracting them together: Android

Also, when sending a question, the logarithm of the error helps to solve the problem faster.

+5
source

Source: https://habr.com/ru/post/1379528/


All Articles