ORMLite和图像保存为BLOB在Android保存为、图像、ORMLite、BLOB

2023-09-06 00:27:51 作者:白衣未央

所以,最近,我将数据库的东西交给ORMLite在我的Andr​​oid平板电脑的应用程序,我写。到目前为止好,得到了最多的事重构/重codeD。虽然我有问题,什么原本存储在数据库中作为BLOB。在我原来的数据模型,它看起来是这样的:

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:

byte[] imageBytes;

但我不认为我可以使用,在ORMLite,最好的,我可以告诉它必须是一个字符串,所以现在我有:

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:

@DatabaseField
String picture;

但现在,我很困惑,如何读取和写入这些数据比特字节,等等......我用code这样的数据运送并从数据库中:

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:

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

所以,现在这就是我是如何保存图像,我不知道如何做到这一点,如果有在ORMLite没有BLOB和我必须用字符串?

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?

的完整性,这是我怎么会显示图像:

for completeness this is how i would display the image:

if(mClient.getImageBytes().length <= 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)));

所以,我有什么做的就是在数据库中,并从这些图像,并为String的字段类型正确的斑点?

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" ?

推荐答案

在ORMLite您确实可以存储字节[] 字段。引述有关字节数组的手动的:

You can indeed store byte[] fields in ORMLite. To quote the manual about byte arrays:

字节数组(字节[])坚持为SQL类型VARBINARY。这是从DataType.SERIALIZABLE类型,序列化对象的字节数组不同。

Array of bytes (byte[]) persisted as SQL type VARBINARY. This is different from the DataType.SERIALIZABLE type which serializes an object as an array of bytes.

请注意:由于向后兼容的,并且是类型byte []的任何字段必须用数据类型字段指定为DataType.BYTE_ARRAY或DataType.SERIALIZABLE并不会自动检测

NOTE: Because of backwards compatibility, any fields that are of type byte[] must be specified as DataType.BYTE_ARRAY or DataType.SERIALIZABLE using the dataType field and will not be auto-detected.

因此​​,使用字节[]你需要指定数据的类型:

So, to use byte[] you will need to specify the type of the data:

@DatabaseField(dataType = DataType.BYTE_ARRAY)
byte[] imageBytes;

诀窍是,ORMLite没有自动检测的类型字节[] 由于一些向后兼容的问题。

The trick is that ORMLite does not automatically detect the type of the byte[] because of some backwards compatibility issues.