从SQLite数据库检索的Android BLOB [英] Retrieving BLOB from SQLite database android
问题描述
我有一些内部的图像保存为BLOB的数据库。
我没有与数据库中的任何问题,到现在为止,我尝试检索BLOB,我会与任何其他数据做的,但它不工作:
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);
问题是,我得到当程序正在试图获得的信息例外c.getBlob(0)。它也没有工作的byte [] B = c.getBlob(0)。
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)".
可能的原因:
- 文件过大? (1.2MB)
- 我要做别的配置的那种BLOB的,这是什么?
谢谢!
解决:当我与它的工作其他文件不小心尝试!后来我才意识到,该文件较小(小于1MB),因此这个问题似乎是尺寸。
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.
现在我的问题是,我可以配置它使BLOB支持大文件?
Now my question is, can I configure it to make the BLOB support large files?
谢谢!
推荐答案
你验证,实际上是在有你的光标数据试图检索任何东西之前?
Did you verify that there is actually data in your cursor before attempting to retrieve anything?
如果(c.getCount()> 0){做某事}
如果您不要做到这一点,那么你会试图检索的东西,已经是空这是一个 BIG否否
if you Don't do this then you will be attempting to retrieve something that is already null which is a BIG NO NO
也做你的元素获取当不会很好的做法来获取这样的: c.getBlob(0)
Also when doing your get of the elements it NOT good practice to retrieve like this: c.getBlob(0)
其他信息
这样做的原因是因为如果你决定要改变你的列和更改订单,这将在很大程度上咬你。
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(字符串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.
查看此处:<一href=\"http://stackoverflow.com/questions/5520175/storing-lists-to-a-database-and-retrieving-them-all-together-android/5520241#5520241\">Storing名单的数据库,并检索它们放在一起:Android的
同时张贴问题时,错误的logcat的帮助客户解决问题的速度更快。
这篇关于从SQLite数据库检索的Android BLOB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!