Cod*_*joy 15 database android ormlite
所以我最近在我正在编写的Android平板电脑应用程序中将我的数据库内容转换为ORMLite.到目前为止一切都很好,大多数事情被重构/重新编码.虽然我对最初作为BLOB存储在数据库中的内容有疑问.在我的原始数据模型中,它看起来像这样:
byte[] imageBytes;
Run Code Online (Sandbox Code Playgroud)
但我不认为我可以在ORMLite中使用它,最好我可以告诉它必须是一个字符串,所以现在我有:
@DatabaseField
String picture;
Run Code Online (Sandbox Code Playgroud)
但现在,我很困惑如何读取和写入这些数据位作为字节等...我使用这样的代码来传输数据到数据库和从数据库传输数据:
...
//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);
Run Code Online (Sandbox Code Playgroud)
那么现在我是如何保存图像的,如果ORMLite中没有BLOBS并且我必须使用字符串,我不知道如何做到这一点?
为了完整性,我将如何显示图像:
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)));
Run Code Online (Sandbox Code Playgroud)
那么我需要做些什么才能将这些图像输入和输出数据库,并且字段类型的字符串是否适合"Blob"?
Gra*_*ray 23
您确实可以byte[]在ORMLite中存储字段.引用有关字节数组的手册:
字节数组(byte [])作为SQL类型VARBINARY持久化.这与DataType.SERIALIZABLE类型不同,后者将对象序列化为字节数组.
注意:由于向后兼容性,任何byte []类型的字段必须使用dataType字段指定为DataType.BYTE_ARRAY或DataType.SERIALIZABLE,并且不会自动检测.
因此,要使用byte [],您需要指定数据的类型:
@DatabaseField(dataType = DataType.BYTE_ARRAY)
byte[] imageBytes;
Run Code Online (Sandbox Code Playgroud)
诀窍在于,byte[]由于某些向后兼容性问题,ORMLite不会自动检测其类型.
| 归档时间: |
|
| 查看次数: |
9954 次 |
| 最近记录: |